简体   繁体   中英

Ember - Initializing table data from ember-data then updating via websocket

I have a table of data that I want to update dynamically.

When the user goes to the page, I want to initialize the table with data from my rails backend. Easy with the model hook and ember data.

I then want to keep this information refreshed using the connected websocket stream.

How should I manage this. Should I be updating the model with the websocket updates (without committing the data to the backend)? The table data is an object array in the component, should I just initialize this from the model setupController function then keep the array updated directly?

Is there an easy way to map the websocket data JSON into the model or table array?

Yes you should be able to do this with Ember Data. Caveat: I have not tried this.

Somewhere you are opening your websocket stream and adding an "onmessage" handler. In that handler, you will receive the payload from the server, where you can use store.pushPayload() to update the record in the store. If the record (identified by the id field) is already in the store, it will be replaced. Otherwise it will be added as a new record. If you are displaying the record in the current template, you should just see the values change when the new record is pushed.

This Ember guide page describes this scenario, where you are streaming data from the server and you want to see instant updates to the user interface.

Additional reading: the API for pushPayload

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM