Socket.IO with Ember and Ember-Data

后端 未结 2 1342
Happy的楠姐
Happy的楠姐 2021-01-30 05:27

I\'ve been poking around and I can\'t find any up to date examples of ember (1.0.0-rc.1) and ember-data(revision 11) that also use socket.io. I\'ve tried something like this.

2条回答
  •  天涯浪人
    2021-01-30 06:14

    There's a very simple solution to this which I'm using in some of my apps. You can either have a general purpose callback for the socket and accept any kind of data

    callback: function(message) {
      // this is better than just `eval`
      var type = Ember.get(Ember.lookup, message.type);
      store.load(type, message.data);
    }
    

    or here it is specifically tailored to your use case

    socket.on('apartment/new', function(apartment) {
      store.load(App.Apartment, apartment);
    });
    

    using store.load will load the record data directly into the identity map. There's also loadMany for loading multiple records.

提交回复
热议问题