2012-05-23 17 views
0

我想要遍歷一個Emebrjs收集 - 每次旁邊的下列項目中選擇一個用戶點擊其屬性更改:如何循環中ember.js陣列集合視圖

如何遍歷列表視圖外面的物品? 看到 -

http://jsfiddle.net/gavriguy/NHxJ4/

var App = Em.Application.create({ 
    ready: function() { 

     App.someItemsView.appendTo('body'); 
     this._super(); 
    } 
}); 

App.tempController = Em.ArrayController.create({ 
    self: this, 
    foo: {title:'tt'}, 
    content: [ 
     { 
     title: 'A', 
     unread: true}, 
    { 
     title: 'B', 
     unread: false}, 
    { 
     title: 'C', 
     unread: false} 
    ], 
    init: function() {}, 

    highlightNext: function() { 

     var currentItem = this.content.filterProperty('unread', false)[0]; 
     //How to change the current item's unread to true??? 

    } 


}); 

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) { 

      this.setPath('content.unread', false); 
     } 
    }), 


});​ 

回答

1

由於App.tempController.content陣列在你的項目是不是Ember.Object的,你不能使用content.set('unread', false)。如果要更改非Ember對象的屬性,可以使用Ember.setPath函數,請參閱http://jsfiddle.net/pangratz666/7RLDr/

highlightNext: function() { 
    // get all unread items 
    var unreadItems = this.content.filterProperty('unread', true); 
    // get the first object 
    var nextUnreadItem = unreadItems.get('firstObject'); 
    // check if there is a object at all 
    if (nextUnreadItem) { 
     // finally set the property 
     Ember.setPath(nextUnreadItem, 'unread', false); 
    } 
}