1

I've got an Ember.ArrayController which contains the results of a DS.Store#find query.

When the app receives a notification from Pusher that a new item has been added, I want to add it to the list:

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!
    }
});

This is similar in nature to the following questions:

[1] Adding item to filtered result from ember-data

[2] create temporarty non persistent object in Ember-Data

Community
  • 1
  • 1
rlivsey
  • 2,124
  • 2
  • 18
  • 21

1 Answers1

0

(1) Use #load to load in records without them being flagged as new.

(2) A DS.AdapterPopulatedModelArray will not allow you to modify it. What I would do is manually create my own DS.ModelArray, and bind the content and isLoaded properties to the one returned by the DS.Store.

Christopher Swasey
  • 10,392
  • 1
  • 31
  • 25