Last active
March 6, 2018 11:23
-
-
Save samuel-alves/46e0fccceb9f07e8adc99b124f6588d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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