Data search ember dataAll returns 0 records on first request

Where is the best way to upload ember strap data to fill the store:

I am currently using revision 13 of ember data.

I'm currently trying to use this in ApplicaitonRoute:

App.ApplicationRoute = Ember.Route.extend setupController: -> App.Contact.find().then (contacts) -> console.log contacts.get('length') 

What I find is that the console.log statement above will write 0, but if I then type:

 App.Contact.find().get('length') 

in the console, the length will be what I would expect, i.e. more than 0.

I was confused about what was going on, I would have thought that the promise would not be resolved until the records were realized.

I went through the code and json figured out the serializer, and I can't figure out why the length is 0 for the first time.

I can’t create a fiddle for this, as this only happens when connecting to our backend store through the remaining adapter.

+2
source share
2 answers

I got this answer after creating a problem on github:

In fact, DS.Model.find () returns the "promoted" RecordArray promise that is allowed when the record is loaded (that is, its property isLoaded true). The problem is that DS.Model.find () is loaded immediately after the array is created, as a result of which the promise is resolved.

To fix your problem (because I think you want to return a live array), something like this should work:

 App.Contact.find({}).then(contacts) -> return App.Contact.all() 

Thus, under the hood, you call findQuery, which loads when the backend returns records, and not when creating an array.

+1
source

Where is the best way to upload ember strap data to fill the store:

The best place is in the model hook (instead of setupController), because the ember router will wait until each model trap solves the problem before moving on to the next one. For example:

 App.ApplicationRoute = Ember.Route.extend model: -> App.Contact.find() 

I was confused about what was going on, I would have thought that the promise would not be resolved until the records were realized.

Agreed that was amazing. I will see if I can find a way to reproduce. One thing you can try is to enable XMLHttpRequests in chrome developer tools so you can make sure that the promise has really been resolved before ajax returns. Thinking that it could be a binding problem, for example, maybe the promise is resolved at the right time, but the length property is not updated until the next cycle of the cycle.

+1
source

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


All Articles