Ember and Ember StateManager data

I have an application that uses ember-data to store data and StateManager to manage its global state. Since data loading is asynchronous, I am wondering how to catch an event that says that all data is loaded. Exactly, I have a state called "loading", where I load data using:

App.store.find(App.Model, ....) 

When the model is loaded, I do some post-processing. This is done in the “download” sub-task named “post-processing”. I move on to "post-processing" when each mode received a "didLoad" event:

 App.Model = DS.Model.extend { didLoad: -> stateManager.send('postProcess',this) } 

When all the data is downloaded and processed by mail, the application should go to another “editing” state, which is at the same level as the “download”:

  loading -> postprocessing root / \ editing 

What event should be caught for the transition to occur? Does the ember-data Store data store have a callback for this?

+4
source share
3 answers

When using ember data, the find method returns a proxy array. You can observe the isLoaded field on this object.

 var data = App.store.find(App.Model, {}); data.addObserver('isLoaded', function() { if (data.get('isLoaded')) { .... } }); 

But remember to clear your observer through removeObserver.

I added this utility to the built-in array of ember data records.

 DS.RecordArray.reopen({ onLoad: function(callback) { if (this.get('isLoaded')) { callback(this); } else { var that = this; var isLoadedFn = function() { if (that.get('isLoaded')) { that.removeObserver('isLoaded', isLoadedFn); callback(that); } } this.addObserver('isLoaded', isLoadedFn); } return this; } } 

So now you can just do

 App.store.find(App.Model, {}).onLoad(function(data) { .... }); 

You can also do something like

 init: function() { this.set('data', App.store.find(App.model, {})); }, onData: function() { if (this.get('data.isLoaded')) { ... } }.observes('data.isLoaded') 
+5
source

Try observing " content@each.isLoaded " and call stateManager.sent ("edit") in the arrayController for models. Sort of:

 App.ModelArrayController.reopen({ didLoadAllContent: function () { if (content.everyProperty('isLoaded')) { stateManager.transitionTo('editing'); } }.observes(" content.@each.isLoaded ") )} 

Maybe easier, of course. Or maybe more correct ...

0
source

As a rule, the state of the model will be processed by the Route.

The model hook performs store.find. http://emberjs.com/api/classes/Ember.Route.html#method_model

After the route model is enabled (loaded), the afterModel hook will be called. http://emberjs.com/api/classes/Ember.Route.html#method_afterModel

0
source

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


All Articles