Skip to content

Instantly share code, notes, and snippets.

@taylorSando
Created October 23, 2012 13:07
Show Gist options
  • Save taylorSando/3938642 to your computer and use it in GitHub Desktop.
Save taylorSando/3938642 to your computer and use it in GitHub Desktop.
Altering backbone to go to /login and /logout
sync: function(method, model, options) {
if (method == "update" || method == "create") {
var params = {type: "POST", dataType: "json", "url": ___ROOT_URL___ + "/login",
contentType: 'application/json', data: JSON.stringify(model.toJSON()),
processData: false
};
return $.ajax(_.extend(params, options));
} else {
Backbone.sync(method, model, options);
}
},
login: function(creds) {
// this.save(creds);
var model = this;
$.ajax({type: 'POST',
url: ___ROOT_URL___ + "/login",
data: {email: "taylor@taylor.com", password: "password"},
success: function(data) { model.set("userID", data); },
error: function(data) { console.log(data);}
});
},
logout: function() {
// Do a DELETE to /session and clear the clientside data
var that = this;
this.destroy({
success: function (model, resp) {
model.clear()
// Set auth to false to trigger a change:auth event
// The server also returns a new csrf token so that
// the user can relogin without refreshing the page
that.set({auth: false, _csrf: resp._csrf});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment