Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a delay to ember fixture data to simulate ajax

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?

like image 219
Sam Selikoff Avatar asked Aug 07 '13 00:08

Sam Selikoff


2 Answers

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.

like image 60
intuitivepixel Avatar answered Nov 15 '22 21:11

intuitivepixel


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!!!
    });
 }
like image 4
Kingpin2k Avatar answered Nov 15 '22 19:11

Kingpin2k