如何使用backbone.js创建模型/用户并将其保存在rails中的数据库中
问问题
83 次
1 回答
0
[Backbone]
Define a User model:
var User = Backbone.Model.extend({
defaults:{
birthday: "Unknown"
},
url: "/users", //create user RESTful service url
validate: function(attrs,options){
if(attrs.name===''){ //simple checking
return "name can't be blank";
}
}
});
//define save callback
function onSuccess(model, response, options){
console.log(response);
}
function onError(model, xhr, options){
console.log("error");
}
//create user instance
var user = new User();
//validation
user.on("invalid",function(model,error){
alert(error);
});
//save data to backend
user.save({name:"Jimmy",age:"12",gender:"male"},{success:onSuccess,error:onError});
By default, backbone using jquery.ajax to make a RESTful JSON request. If you want to use other method, you have to override Backbone.sync.
Hope this is helpful for you.
于 2013-07-11T06:22:33.640 回答