Backbone sending parameters over AJAX to server
Hey, I'm very new to backbone, and I've read a little bit of the documentation but I didn't find something appropriate. I would like to use backbone to send some data to the server using AJAX communication. Does backbone have some kind of shortc开发者_JAVA百科ut to do this? For now my simple example consist of a HTML page with an input for username and one for the password. On clicking the button I fire this function:
//handling the button event on the login form
function loginPressed(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
loginModel.username = username;
loginModel.password = password;
}
The login model is just a model I've created with backbone:
//creating a backbone model to handle login
var loginModel = new Backbone.Model({
username: "",
password: "",
});
Do you think I can do AJAX request to my server with this?
Thanks, Masiar
P.S. My serverside is node.js
Your model is not defined properly:
var LoginModel = Backbone.Model.extend({
url: "/your/login/url"
});
...
function loginPressed(){
var login = new LoginModel
login.save({
username: document.getElementById("username").value,
password: document.getElementById("password").value
},{
/*any other jquery ajax options including callbacks*/
});
}
There are some reasons you should not do login with ajax calls but you'll learn them soon enough :)
精彩评论