json - Populating ember-data table from WebSocket -
my backend sending me json (example):
{ id: 0, list: [ {username:'user_1', name:'name1', surname:'surname1', user:0}, {username:'user_2', name:'name2', surname:'surname2', user:0}, ] }
i have necessity bootstrap table app.wuser
type of json. data comes via websocket. ember.js schema defined follow:
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') });
i writed custum adapter websocket implementation. note app.websocketconnection
mixin contains connection handler (see last snippet).
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'} });
my store
app.store = ds.store.extend({ revision: 12, adapter: ds.socketadapter.create({}) });
my 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 server /all ' + data); // <---- ??? }; this._super(); }, send: function(data) { // not relevant method... method send message server /* var = ... this.ws.send( json.stringify( ) ); */ } });
searching on google, reached write piece of code:
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 specify [0] id? (?) this.didcreaterecords(store, app.wuser, records(?), undefined); // neccessary line?
questions:
- where put piece of code?
- is piece of code correct? correct way boostrap data?
- is correct usage of custum primarykey?
- considering custum id of table
app.user
; correct call loadmany() function? - note json coming onmessage callback (see line
`// <---- ???
). how call 1 of functions - , - (i.e.createrecords) inherited adapter, passing json data?
i bit confused, hope in right way... problem glue pieces of code!
thanks patience!
mattia
it looks 1 app.wuser
@ time.
so this
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 json in store. }; this._super(); },
Comments
Post a Comment