如何在ember.js中迭代数组集合视图

时间:2012-05-23 09:48:06

标签: ember.js

我想迭代Emebrjs集合 - 每次用户点击下一步时,选择以下项目并更改其属性:

如何从外部迭代列表视图项? 见 -

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 个答案:

答案 0 :(得分: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);
    }
}