Skip to content

Instantly share code, notes, and snippets.

@samuel-alves
Last active March 6, 2018 11:23
Show Gist options
  • Save samuel-alves/46e0fccceb9f07e8adc99b124f6588d3 to your computer and use it in GitHub Desktop.
Save samuel-alves/46e0fccceb9f07e8adc99b124f6588d3 to your computer and use it in GitHub Desktop.
var Books = Backbone.Collection.extend({
// Lets create function which will return the custom URL based on the method type
getCustomUrl: function (method) {
switch (method) {
case 'read':
return 'http://localhost:3000/api/Books/' + this.id;
break;
case 'create':
return 'http://localhost:3000/api/Books';
break;
case 'update':
return 'http://localhost:3000/api/Books/' + this.id;
break;
case 'delete':
return 'http://localhost:3000/api/Books/' + this.id;
break;
}
},
// Now lets override the sync function to use our custom URLs
sync: function (method, model, options) {
options || (options = {});
options.url = this.getCustomUrl(method.toLowerCase());
// Lets notify backbone to use our URLs and do follow default course
return Backbone.sync.apply(this, arguments);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment