1

I'm trying to pull in a livestream of data from a socket.io websocket, coming in as JSON.

I was trying to use the method from these folks, but no luck (I'm getting error - "Uncaught TypeError: Cannot call method 'load' of undefined" - which I haven't been able to figure out on my own):

Socket.IO with Ember and Ember-Data

My code:

var socket = io.connect('http://localhost:8007');
  socket.on('my_live_stream', function (data) {
   store.load(App.Group, data);
  });

And more:

App.Group = DS.Model.extend({
id: DS.attr('string'),
name: DS.attr('string'),
usage: DS.attr('string'),
sunshine: DS.attr('string'),    
device_info: DS.attr('string')
});

Edit: What the JSON looks like...

{
"group":{
"usage":{
    "case1":0,
    "case2":0,
    "case3":0

},
"sunshine":"00/00/0000",
"id":1010,
"device_info":11.5,
...

I'm still very new to Ember here, but I'm just trying to get {{name}}, {{usage}}. and {{device_info}} to my Index template. I see a great stream of data when I add console.log(data) to the socket code (to replace store.load...). What's the next step?

Thanks so much!

Community
  • 1
  • 1
ptep
  • 35
  • 9
  • What do you expect the variable `store` to be in the first code you posted? – Ian Henry Mar 31 '14 at 19:11
  • I thought, perhaps mistakenly, I would get an array of entries from my livestream - that then would be available to my template. I've added an example of the json to the above. – ptep Mar 31 '14 at 19:51
  • That's `data`, though. Your error originates from `store` being undefined. It's not a global variable; you need to refer to a specific store instance. – Ian Henry Mar 31 '14 at 20:04

1 Answers1

1

The question you reference is using a very outdated version of Ember Data. You should pretty much ignore any Stack Overflows from before October.

The new method you want is store.push, in your case, in your case store.push('Group', data).

However, there's yet another problem in your code, which is that you don't have access to store in that context. Normally, you access the store inside routes and controllers via this.store. However, you're not inside a route or controller. If you want, you could hack access to the store like so, store = App.__container__.lookup('store:main'), but this is not the Ember way and will probably cause you problems down the line. Instead, you could add it to one of the hooks in the Application Route, where you do have access to this.store.

For example, you could set it up like this:

App.ApplicationRoute = Ember.Route.extend({
    activate: function(){
        var that = this;
        var socket = io.connect('http://localhost:8007');
        socket.on('my_live_stream', function (data) {
            that.store.push('Group', data.group);
        });
    })
});

Docs: http://emberjs.com/guides/models/pushing-records-into-the-store/

joegoldbeck
  • 526
  • 3
  • 11
  • True, but the problem here is that `store` isn't even defined. – Ian Henry Mar 31 '14 at 20:03
  • Nice not see errors (for once...). So I thought the next step would be easy - throwing it on a template with a {{#each controller}} {{device_info}} - but no dice (nothing appears). Mind pointing me in the right direction? – ptep Mar 31 '14 at 22:04
  • Hard to say without seeing your code, but the each helper is if your model is an array of things. If it's not an array, then you should be able to do {{device_info}}. Take a look in the ember inspector (chrome addon), and you can make sure that your data is loaded in and formatted as you expect. If that's not enough info, feel free to make a new question (perhaps with a jsbin) - hard to really dig into things in comments. – joegoldbeck Mar 31 '14 at 23:27
  • Thanks, jgoldbeck - I appreciate the patience! I am getting an error as it turns out - "Uncaught Error: Assertion Failed: You must include an `id` in a hash passed to `push`" - tried adding "that.store.push('Group','id', data); - but nothing. The model is an array of things - every so often, I'm fed a "Mon Mar 31 2014 20:50:11 GMT-0400 (EDT): group update:" for a unique group id. – ptep Apr 01 '14 at 00:49
  • That error means that data should contain a property `id`, which corresponds to the id of the Group you are pushing onto the store. By 'hash' they just mean the js object – joegoldbeck Apr 01 '14 at 00:52
  • New question posted - thanks - http://stackoverflow.com/questions/22787291/ember-websocket-where-are-my-records – ptep Apr 01 '14 at 13:25