Backbone.Service aims to help with the cases when restul API is not an option.
<script src="https://github.com/mkuklis/backbone.service/raw/master/backbone.service.js"></script>
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' });
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
}
});
(The MIT License) Copyright (c) 2012 Michal Kuklis