Request model data in the Ember.js route

Is there a way to do real-time data using Ember.js?

What I'm looking for is the ability to insert updated and new records (rather than deleting) into previously returned results.

So, let's say I have routers that look like this, which allows sorting and swap (aka skip):

App.Router.map(function() { this.resource('messages', { path: '/messages/skip/:skip/sort/:sort/direction/:direction' }); }); App.MessagesRoute = Ember.Route.extend({ model: function(params) { this.set('params', params); return this.query(); }, query: function() { var sort = {}; sort[this.get('params').sort] = parseInt(this.get('params').direction); return App.Message.find({}, { skip: this.get('params').skip, sort: sort }); }, setupController: function(controller, model) { var self = this; this._super(controller, model); Ember.Instrumentation.subscribe('onMessage', { before: function(name, timestamp, message) { self.controller.set('content', self.query()); }, after: function() {} }); }, }); 

This works great - it sorts and skips correctly in a static sense.

(NOTE: I am NOT using Ember Data - just a collection of Ember objects.)

However, the messages are received continuously, and I want the display of these messages to automatically request a new message when they arrive.

I have a websocket that tells me when this happens and it works correctly, and I use the Ember.Instrumentation infrastructure to route this event to close the setupController that I have. But when I do

 this.controller.set('content', this.query()); 

In response to this event, to reload, the content disappears. What am I doing wrong?

+6
source share
2 answers

I finally figured it out, although I would like an ember expert to tell me why.

If I use promises and then () to get the real result and then install it on the controller, it works:

 setupController: function(controller, model) { var self = this; this._super(controller, model); Ember.Instrumentation.subscribe('onMessage', { before: function(name, timestamp, message) { var p = self.query(); p.then(function(response) { self.controller.set('content', response); }); }, after: function() {} }); }, 

I would like to understand why, because I thought Amber could make a promise in addition to real value. However, this solution is actually more enjoyable because there is no blinking, while the REST callback is inverted - the data only gets replaced when it is complete.

+3
source

I think one way is to periodically execute App.Message.find () - ember will handle updates to your views

 window.setInterval(function(){App.Message.find()},1000); 

correct me if i'm wrong - i'm still learning ember

+4
source

Source: https://habr.com/ru/post/947121/


All Articles