Setting a global REST root url in a Backbone.js application

backbone.js

Solution

Not sure what exactly you mean setting `urlRoot` for every model manually. Presumably you do

var MyModel = Backbone.Model.extend({

  urlRoot: '/mymodel',
  ...

});

right? Then each model instance naturally has the same `urlRoot`.Any other model instance of say `MyOtherModel` would have some different `urlRoot`.

If for some reason you need to be using the same `urlRoot`, I would guess it would be because the models share attributes. This should hint to inheritance or extension so you could do:

var BaseModel = Backbone.Model.extend({
  urlRoot: '/mymodel'
});

var MyModel = BaseModel.extend({
  ...
});

Problem

In backbone.js, you have to set the rooturl of every model manually. Is there a way we can set this in a single location once and all models will use it? For eg. `api.site.com` will be the REST service, but for testing purpose, it may be at `localhost:1000` I need to be able to change the root url of the service easily and not have them all over the place in the many models that exist in the application.

Original source