Model.find (). Then () fires while the records are actually loaded

I would like to download the whole collection, and then just clear the records so that they can be used as models one at a time, without having to go back to the server each time.

I figured out how to use Ember.Deferred to return a promise, but I can’t get the promise to allow at the right time. The following code only displays "Found 0":

App.PersonRoute = Ember.Route.extend({ model: function(params) { var name = "Erik"; var promise = Ember.Deferred.create(); App.people = App.Person.find(); App.people.then(function() { console.log('Found ' + App.people.get('length')); var person = App.people.findProperty('name', name) promise.resolve(person); }); return promise; } }); 

If I wrap the body of then () in setTimeout and make it wait a couple of seconds, everything will be fine.

Is there any other event that I can relate to? I tried App.people.on ('isLoaded'), but isLoaded is always right.

Thanks!

+6
source share
2 answers

Is there any other event that I can relate to?

Indeed, there is an event that you can listen to, and that didLoad .

I tried App.people.on ('isLoaded'), but isLoaded is always right.

As for isLoaded , there was a lot of confusion here; see here ), confusion arises because the isLoaded flag is isLoaded to true in content when the repository finished loading RecordArray for records, even if it was initially empty because the record was no longer available locally. Then, when the server request is returned, RecordArray will be populated with records received from the backend, and the bindings will begin and your templates will be updated.

As indicated in guides :

A record that is downloaded and clean means that it received information about its attributes and relations with the server, and no changes were made locally on the client.

It has been pointed out above that makes didLoad fire.

For additional events related to the model, you can listen to the model life cycle guide

Now for your setup, you can rewrite your code to something like this:

 App.PersonRoute = Ember.Route.extend({ model: function(params) { var name = "Erik"; var promise = Ember.Deferred.create(); App.people = App.Person.find(); App.people.on('didLoad', function() { console.log('Found ' + App.people.get('length')); var person = App.people.findProperty('name', name) promise.resolve(person); }); return promise; } }); 

Hope this helps.

+6
source

If the promise would not be resolved at the right time, I think it would be a (rather large) error in Ember / data. I would suggest filling out the Emberjs / data error.

I suspect that your use of different promises may cause this error.

App.Person.find already returning a promise. You should use this promise when returning from model() . Also, to resolve this promise, you simply return the result.

The implementation style for Promises that you used is usually needed when you integrate an external asynchronous api that does not support Promises .

I would reorganize your model() like this. This can solve the problem of asynchronous synchronization.

 App.PersonRoute = Ember.Route.extend({ model: function(params) { var name = "Erik"; var promise = App.Person.find(); promise.then(function() { console.log('Found ' + App.people.get('length')); return App.people.findProperty('name', name) }); return promise; } }); 

I found the documents in the Q library very useful in determining the best way to use promises. Ember uses RSVP, which is a different library, but the principles are similar.

+2
source

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


All Articles