如何创建模型/用户并使用backbone.js将数据库保存在rails中
答案 0 :(得分:0)
<强> [骨干] 强>
定义用户模型:
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});
默认情况下,骨干使用jquery.ajax发出RESTful JSON请求。如果要使用其他方法,则必须覆盖Backbone.sync。
希望这对你有所帮助。