2

I'm writing an app which leverages Ember.js, with Ember Data, and Socket.IO through Node.js. I'm trying to figure out the best way to receive data from a socket response and load it into my Ember Data store, such as this:

window.socket = io.connect('http://localhost');

socket.on('action here', function(response) {
  // how to load "response" JSON data into Ember Data store from here...?
});

Is the best (or only good) solution to write a custom DS.Adapter? Or is there another good, maintainable way to accomplish this?

Alex Coleman
  • 601
  • 7
  • 18

2 Answers2

4

The best way is to write your own adaptor, but if you only need to load models(no save) you can get away with the adaptor's load method - something like this:

socket.on('single-model', function(response) {
    DS.defaultStore.load(response);
});

socket.on('multiple-models', function(response) {
    for(i in response){
        DS.defaultStore.load(response[i]);
    }
});

Define the adaptor as FixtureAdaptor.

Shimon Rachlenko
  • 5,469
  • 40
  • 51
1

For the record, I ended up going with the answer proposed here:

https://stackoverflow.com/a/14508316/1470194

Using that Ember.Instrumentation technique, I was able to facilitate calls to my controllers and access my store that way.

I realize that writing a custom adaptor may be necessary in many case, but it was beyond the required scope of my project, so this solution ended up being perfect.

Community
  • 1
  • 1
Alex Coleman
  • 601
  • 7
  • 18