Loading sideloaded data received through a WebSocket

I’m trying to load records based on messages received through a WebSocket connection (using Faye). In BREAKING_CHANGES.md it is suggested to use load() on the adapter for this precise use case. It also mentions the API supports sideloaded data, but I don’t see how this would work because load() expects a payload without a root. Am I misunderstanding the documentation or hasn’t the functionality been implemented yet? Is there another method on the store or adapter that I would need to invoke to load sideloaded data based on a payload?

Loading Data

Previously, some features of the store, such as load(), assumed a single adapter.

If you want to load data from your backend without the application asking for it (for example, through a WebSockets stream), use this API:

store.adapterForType(App.Person).load(store, App.Person, payload);

This API will also handle sideloaded and embedded data. We plan to add a more convenient version of this API in the future.

This stumped me for a while, too. The best approach I’ve found is to look at how find works, and to immitate it by calling didFindRecord.

var id = json["person"]["id"];
var store = DS.get("defaultStore");
var adapter = store.adapterForType(App.Person);
adapter.didFindRecord(store, App.Person, json, id);
var person = App.Person.find(id);

This is probably not a best practice.

Instead of didFindRecord, you can try using DS.get('defaultStore).load

I’m working on a project with live comment updates using Faye and Rails. Here’s my comments controller:

class App.CommentsController extends Ember.ArrayController
  init: ->
    client.subscribe('/comments/all', @addComment)
  addComment: (data) ->
    #Add the new comment from the socket and become clean
    store = DS.get('defaultStore')
    store.load(App.Comment, data.comment)

If anyone else can shed light on possible problems with this, that would be wonderful!

1 Like

Loading data directly into the store will bypass deserialization that’s normally done in the adapter layer. Custom aliases, pluralizations, and transformation rules (e.g. for dates) will all be ignored. If you’re receiving data from Faye in the same format served up by your REST server, you’ll probably want to load it through the adapter instead of straight into the store.

I know this thread is pretty old, but are there any good examples of how to use an adapter with updates from Faye? I’m working on a project that has an update that is only one portion of the original sideloaded data. I’m not sure how to update only those elements, as they’ve gone through an excessive amount of Ember Data magic. Any pointers would be appreciated!

Check out the Ember docs on this subject:

socket.on('message', function (message) {
  var type = store.modelFor(message.model);
  var serializer = store.serializerFor(type.typeKey);
  var record = serializer.extractSingle(store, type, message.data);
  store.push(message.model, record);

Link in prev. post is broken, here is a new one: Pushing Records into the Store - Models - Ember Guides