Reputation: 65
I am working with a Backbone.js framework, where I am using dot net Web API (which is created by me only). I am call the API Controller from fetch method of backbone. If this api does not respond in 30 Second i want to show some error message. Where should i set this time out period and how ?
Upvotes: 1
Views: 1868
Reputation: 12093
You could just manually set the timeout with all of your fetch and save calls, like so:
MyModelInstance.fetch({
timeout: 6000
});
If you want a specific timeout globally, however, an easier thing to do is to wrap the Backbone.sync
method when you first load Backbone. Try this:
//Set the default options for all our sync calls - in this case, we're only messing with the timeout
var syncDefaultOptions = {
timeout: 8000
};
//Save a copy of the default Backbone.sync function
var sync = Backbone.sync;
//Wrap the default Backbone.sync function with one that automatically applies our default options, but still allows them to be overwritten manually
Backbone.sync = function(method, model, options) {
options = _.defaults(options || {}, syncDefaultOptions);
sync(method, model, options);
};
Please note that the above code depends on lodash (_.defaults()
), but you could probably replicate it without that dependency fairly easily
Upvotes: 1
Reputation: 33364
By default, Backbone uses jQuery.ajax
to make its requests, which means you can set default values with jQuery.ajaxSetup
. In your case, you would set the timeout option :
timeout
Set a timeout (in milliseconds) for the request.
Try
$.ajaxSetup({
timeout: 30000
});
Upvotes: 1