1

我有一个Ember.ArrayController包含DS.Store#find查询结果的内容。

当应用程序从 Pusher 收到新项目已添加的通知时,我想将其添加到列表中:

App.Item = DS.Model.extend({
    name: DS.attr("string")
});

App.latestItemsController = Ember.ArrayController.create({

    init: function() {
        // load a page of data from the server
        this.set("content", App.store.find(App.Item, {page: 1}));

        // subscribe to events from our app-wide message pump 
        // which dispatches events from Pusher
        App.get("eventQueue").on("item-added", _(this.itemAdded).bind(this));
    },

    // called whenever we're notified of a new item being added by someone else
    itemAdded: function(data) {

        // don't want to do this as it'll save next time we do a App.store.commit();
        App.store.createRecord(App.Item, data);

        // can't do this because it's a DS.Model and tells you to use createRecord:
        // plus as it's a filtered result even if the App.Item.create succeeded 
        // then we'd get an error because this.content is immutable [1]
        this.pushObject(App.Item.create(data));

        // ... not sure what else to try!
    }
});

这在本质上类似于以下问题:

[1]将项目添加到来自 ember-data 的过滤结果

[2]在 Ember-Data 中创建临时非持久对象

4

1 回答 1

0

(1) 使用#load 加载记录而不将它们标记为新记录。

(2) DS.AdapterPopulatedModelArray 不允许您修改它。我要做的是手动创建自己的 DS.ModelArray,并将内容和 isLoaded 属性绑定到 DS.Store 返回的属性。

于 2012-04-07T22:59:12.247 回答