backbone.service
backbone.service copied to clipboard
Backbone service for non restful or semi restful apis
trafficstars
Backbone.Service
Backbone.Service aims to help with the cases when restul API is not an option.
Install
<script src="backbone.service.js"></script>
Usage
You can use backbone.service as a standalone object or extend backbone model or collection.
// define server targets / endpoints
var targets = {
login: ["/login", "post"],
signup: ["/signup", "post"],
logout: ["/logout", "get"],
search: "/search" // defaults to get
resetPassword: ["/resetpassword", "post"],
updateSettings: ["/updateSettings", "post"]
};
// standalone service
var service = new Backbone.Service({ url: "http://localhost:5000", targets: targets });
// extend backbone model
var User = Backbone.Model.extend(service);
Each target passed to Backbone.Service becomes a method on the model or collection.
User model has now access to new methods: login, signup, logout, search, resetPassword, updateSettings.
Each new method takes two arguments: data and options.
You can use it like this:
var user = new User();
user.login({ username: 'bob', password: 'secret' });
Promises / Callbacks
Backbone.service comes with a simple implementation of promises. You can use them like this:
user.updateSettings(settings).then(function (res) {
// do something after successful update
}, function (err, res) {
// do something in case of an error
});
Callbacks are still supported. You can pass them as a second argument in your calls:
user.updateSettings(settings, {
success: function (res) {
// do something after successful update
},
error: function (err, res) {
// do something in case of an error
}
});
##Contributors
##License:
(The MIT License) Copyright (c) 2012 Michal Kuklis
