2013-05-02 25 views
3

我的後臺是給我這個JSON(例如)填充燼數據表:從WebSocket的

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

我有必要引導我的表App.WUser與該類型JSON的。數據來自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({}) 
}); 

我的密新:

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)); 
    */ 
    } 

}); 

搜索在谷歌,我伸手寫這段代碼:

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. 考慮到我的客戶ID爲App.User;調用loadMany()函數是否正確?
  5. 請注意,JSON來自onmessage回調(請參見行`// <---- ???)。如何調用適配器繼承的函數之一 - 以及哪些 - (即createRecords),並將JSON數據傳遞給它?

我有點困惑,但我希望我是在正確的方式......我的問題是粘合我的代碼塊!

感謝您的耐心等待!

馬蒂亞

回答

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(); 
    }, 
+0

Thaks這麼多!它的作用就像一個魅力:[見鏈接](http://img689.imageshack.us/img689/9505/snapshot6t.png)。一個問題是我的外鍵看起來是空的(並且被命名爲** user_id **,而不是**用戶**)。我認爲是因爲我在我的Adapter之外序列化了我的記錄。 (請注意,我不使用自定義序列化器,我應該嗎?) – Mattia 2013-05-03 19:18:01

+0

您想要指定用戶在App.Adapter.map('App.WUser',{0128} :'load'} };請參閱https://github.com/emberjs/data/blob/master/packages/ember-data/tests/integration/embedded/embedded_loading_test.js#L92 – 2013-05-03 19:21:49

+0

,您還需要使用'inverse'使得數據可以發現'list'是用戶存儲的地方。請參閱https://github.com/emberjs/data/blob/master/packages/ember-data/tests/integration/inverse_relationships_test.js# L65 – 2013-05-03 19:23:09