Add delay to ember fixture data to simulate ajax

I use Ember fixtures to prototype my application, and I would like to add a delay when the model is requested to simulate an ajax request. Is it possible?

+6
source share
2 answers
Good question. FixtureAdapter has a built-in function, the property that you need to configure is called latency . Try the following:
 App.Store = DS.Store.extend({ adapter: DS.FixtureAdapter.create({ latency: 5000 }); }); 

This will add a delay of 5000 milliseconds (5 seconds) to the FixtureAdapter , waiting 5 seconds before it returns the data.

Although @Daniel’s answer is also a valid approach, using this built-in function will allow your model not to touch, which will not require any effort when you switch to another adapter at some point and there will be no need to remove the simulated promises, which will lead to creating cleaner code.

Hope this helps.

+6
source

In your model, return a promise that will be resolved after a short delay.

  model: function(params, transition){ return new Ember.RSVP.Promise(function(resolve){ setTimeout(function(){ var model = App.Model.find(params.id); resolve(model); }, 3000); // 3 second delay, wooh, your server is slow!!! }); } 
+4
source

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


All Articles