I'm using Ember fixtures to prototype my app, and I'd like to add a delay the first time a model is requested to simulate an ajax request. Is this possible?
Good question. The FixtureAdapter
has that exact feature built-in, the property you need to configure is called latency
. Try this:
App.Store = DS.Store.extend({
adapter: DS.FixtureAdapter.create({ latency: 5000 });
});
This will add a delay of 5000 millis (5 seconds) to the FixtureAdapter
, waiting 5 seconds before it returns the data.
Although @Daniel's answer is also a valid approach, but using this built-in feature will let your model hook's untouched which will require no effort at all when you switch to a different Adapter at some point, and there will be no need to remove the simulating promises resulting in cleaner code.
Hope it helps.
In your model hook return a promise that resolves 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!!!
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With