根据你的代码,App.EventsController
有一个事件列表,现在让我们说我们希望UI有一个显示事件的列表,并且对于每个事件,我们希望该视图具有一个删除按钮,该按钮删除该事件时用户点击
一种方法是使用Ember.CollectionView
,集合视图顾名思义是为这些类型的需求量身定做的,在许多Ember示例中,视图的用法没有被定义,因为烬会为你自动生成它但在某些情况下,我们可能需要明确定义视图来满足我们的要求
App.EventsView = Ember.CollectionView.extend({
// It needs a list of data to iterate upon
// We are binding it to the controllers content data which
// is a list of events
contentBinding: "controller.content",
appendSpan: function(){
view = Ember.View.create({tagName: 'span'});
this.get("childViews").forEach(function(child){
view.appendTo(child);
});
},
// Now we need to also define a view template that will be
// generated for all the elements in the content array
// This could in turn be another collection view if required
// I am going to keep it simple for now
itemViewClass: Ember.View.extend({
templateName: "event",
deleteEvent: function(){
// Implement Delete
this.get("content").deleteRecord();
},
notifyUser: function(){
// The record doesn't get deleted as soon as user clicks, the request goes to
// server and server deletes the record and sends back the response to the
// client, Hence I'm using an observer here on isDeleted property of the record
if(this.get('content.isDeleted')){
alert("Record deleted Successfully");
}
}.observes('content.isDeleted')
})
})
重要提示里面的的CollectionView定义this.get("content")
指事件的阵列,而在itemViewClassthis.get("content")
指的是单个事件对象现在
//Defining the template
//Assuming the event model has a name property
<script type="text/x-handlebars" data-template-name="event">
Name: {{view.content.name}}
<a {{action deleteEvent target="view"}} href="#">Delete Event</a>
</script>
当你打你的APPLICATION_URL /事件 你”我希望这可以清除一些概念
有关CollectionView的更多信息
更新按照注释:
如果要追加另一种观点认为每个孩子视图,您可以通过编辑itemViewClass的模板,这样做如下
<script type="text/x-handlebars" data-template-name="event">
Name: {{view.content.name}}
<a {{action deleteEvent target="view"}} href="#">Delete Event</a>
{{ view App.SomeOtherView }}
</script>
它可以是一个部分过如下
<script type="text/x-handlebars" data-template-name="event">
Name: {{view.content.name}}
<a {{action deleteEvent target="view"}} href="#">Delete Event</a>
{{ partial "somePartial" }}
</script>
,或者如果你想这样做编程的说,你在EventsView模板点击一个按钮,点击所有孩子的看法米乌斯季有附加到它的跨度标签(我非常不擅长给人的例子)
//This is the template for App.EventsController,
//template-name = "events" => view is App.EventsView and controller App.EventsController
<script type="text/x-handlebars" data-template-name="events">
<a {{action appendSpan target="view"}} href="#"> Append </a>
</script>
appendSpan
在的CollectionView
极好的响应定义。这意味着我可以直接调用{{view“ScheduleApp.TripsView”}}并且ember将加载所有的视图。所以view.content是每个Trip,但内容仍然是数组,控制器仍然是TripsController。那么如何在这些生成的视图上调用appendTo? – 2013-02-28 20:38:44
纠正其{{view ScheduleApp.TripsView}},再次TripsView是Ember.CollectionView的一个实例,然后在TripsView中view.content引用Array,但里面TripsView的itemViewClass view.content引用单个Trip,查看关于添加的更新答案查看,我不确定你是否要求相同,如果不是,你可以发布关于你的用例的小提琴吗? – 2013-03-01 06:29:38