documentcloud/backbone — Issue #687: Better native support for non-REST applications
Author: dswitzer
Created: 2011-10-24 20:41:59+00:00
State: open

I'd like to see Backbone have better support when REST isn't being used in an application. Some of the methods (like Collection.create) have dependencies on the Backbone.sync and a URL isn't defined on an object an error is thrown.

Currently I've been using something like the following to skip trying to run sync operations when no URL is found:

```js
(function (){
var originalBackboneSync = Backbone.sync;
Backbone.sync = function (method, model, options){
var url = (!(model && model.url)) ? null : _.isFunction(model.url) && (model.url !== Backbone.Model.prototype.url) ? model.url() : null;
if( !url ){
options.success(model);
return true;
} else {
originalBackboneSync.apply(this, arguments);
}
};
})();
```

I'd love to see some way to just tell Backbone that if no "URL" is present to not throw an error, but instead just process the success callback natively.

While I understand REST is a huge part of Backbone, not every application platform supports it and conversion can take a while.

Maybe the best solution would be to implement a Backbone.noREST flag. By default this is false and the behavior would continue to throw an error. If set to true, then the Backbone.sync would just run the success callback and not attempt to contact the server if no URL is defined. If a URL is defined for an object, then it would behave normally.