2012-05-22 43 views
1

我有灰燼的應用程序有兩種視圖和控制器:如何操作列表項從它的觀點在emberjs

http://jsfiddle.net/gavriguy/EDr4G/

我要標記的當前項目的用戶的點擊閱讀 - 由改變它的相關模型。 我目前能夠通過計算視圖的項目索引來做到這一點 - 但問題是我無法確定視圖上的索引與其控制器上的索引是否相同。 有什麼想法?

的JavaScript

App.tempController = Em.ArrayController.create({ 
    content: [ 
     { 
     title: 'A', 
     unread: true}, 
    { 
     title: 'B', 
     unread: true}, 
    { 
     title: 'C', 
     unread: false} 
    ] 
}); 

App.someItemsView = Ember.CollectionView.create({ 
    contentBinding: 'App.tempController.content', 
    itemViewClass: Ember.View.extend({ 
     template: Ember.Handlebars.compile('<div>{{content.title}} unread: {{content.unread}}</div>'), 
     click: function(event) { 
      //How to mark current clicked item as read? 
      console.log(this.content); 
      console.log(event); 
      this.set('content.unread', false); 
     } 
    }) 
});​ 

回答

2

裏面你click處理程序,你可以得到的參考,爲該視圖通過this.get('content')呈現的數組項。所以,你可以通過this.setPath('content.unread', false)設置標誌,看http://jsfiddle.net/pangratz666/t6Nst/

itemViewClass: Ember.View.extend({ 
    template: Ember.Handlebars.compile('<div>{{content.title}} unread: {{content.unread}}</div>'), 
    click: function(event) { 
     // this.content is the item in the array on which this click event occures. 
     this.setPath('content.unread', false); 
    } 
}) 
+0

謝謝,它的工作原理就像一個魅力:) – Gavriguy