从WebSocket填充ember-data表

时间:2013-05-02 19:56:57

标签: json ember.js websocket ember-data

我的后端正在向我发送这个JSON(示例):

{
  id: 0,
  list: [
         {username:'user_1', name:'Name1', surname:'Surname1', user:0},
         {username:'user_2', name:'Name2', surname:'Surname2', user:0},
        ]
}

我有必要使用那种类型的JSON来引导我的表App.WUser。数据来自WebSocket。我的Ember.js架构定义如下:

App.WUser = DS.Model.extend({
  list: DS.hasMany('App.User')
});

App.User = DS.Model.extend({
  username: DS.attr('string'), // primary key
  name: DS.attr('string'),
  surname: DS.attr('string'),
  user: DS.belongsTo('App.WUser')
});

我写了我的custum Adapter for WebSocket实现。请注意,App.WebSocketConnection是一个包含连接处理程序的Mixin(请参阅最后一个代码段)。

DS.SocketAdapter = DS.RESTAdapter.extend(App.WebSocketConnection, { 
  socket: undefined,

  init: function() {    
    socket = this.getSocket(); // activate WS connection (see Mixin)
    this._super();
  },

  find: function (store, type, id) {
     // empty block    
  },

  findAll: function (store, type) { 
    // empty block
  },

  createRecord: function(store, type, record) {
    // code not relevant
  }
});



DS.SocketAdapter.map('App.User', {
    primaryKey: 'username'
});


DS.SocketAdapter.map('App.WUser', {
  list: {embedded: 'always'}
});

我的商店

App.Store = DS.Store.extend({
  revision: 12,
  adapter: DS.SocketAdapter.create({})
});

我的Mixin:

App.WebSocketHandler = Ember.Object.extend({
  uri: 'ws://localhost:8080/App/atmosphere/chat/all',
  ws: ,

  init: function() { 
    this.ws = new WebSocket(this.uri));                                                                 

    // callbacks
    this.ws.onopen = function() {
      console.log('Connection established /all');
    };
    this.ws.onclone = function() {
      console.log('Connection closed /' + 'all');
    };
    this.ws.onmessage = function(data) {
      alert('New JSON message from server /all ' + data);    // <---- ???
    };

    this._super();
  },

  send: function(data) { 


    // not relevant method... this method simply send a message to the server
    /* 
   var some = ...

    this.ws.send( JSON.stringify( some ) );
    */
  }

});

在Google上搜索,我写了这段代码:

var JSONfromWebSocket = {
  id: 0,
  list: [
     {username:'user_1', name:'Name1', surname:'Surname1', user:0},
     {username:'user_2', name:'Name2', surname:'Surname2', user:0},
    ]
};
    var store = DS.get('defaultStore');
    store.loadMany(App.WUser, [0], JSONfromWebSocket); // should I specify the [0] id?

    (?) this.didCreateRecords(store, App.WUser, records(?), undefined); // is it a neccessary line?

问题:

  1. 我把我的代码放在哪里?
  2. 我的代码是否正确?这是提升数据的正确方法吗?
  3. 使用custum primaryKey是否正确?
  4. 考虑我的表App.User的密码ID;是否正确调用loadMany()函数?
  5. 请注意,JSON来自 onmessage 回调(请参阅第`// <---- ???行)。如何调用适配器继承的函数之一 - 哪个 - (iecreateRecords),传递JSON数据?
  6. 我有点困惑,但我希望我的方式正确...我的问题是粘合我的代码片段!

    感谢您的耐心等待!

    马蒂亚

1 个答案:

答案 0 :(得分:3)

看起来你一次只能获得一个App.WUser

这样的事情

App.WebSocketHandler = Ember.Object.extend({
  uri: 'ws://localhost:8080/App/atmosphere/chat/all',
  ws: ,

  init: function() { 
    this.ws = new WebSocket(this.uri));                                                                 

    // callbacks
    this.ws.onopen = function() {
      console.log('Connection established /all');
    };
    this.ws.onclone = function() {
      console.log('Connection closed /' + 'all');
    };
    this.ws.onmessage = function(data) {
      DS.get('defaultStore').load(App.WUser, data);  //Simply load your json in the store.
    };

    this._super();
  },