How to create a user and save it the DB in rails using backbone.js -
how create model/user , save db in rails using backbone.js
[backbone]
define 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 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 backend user.save({name:"jimmy",age:"12",gender:"male"},{success:onsuccess,error:onerror});
by default, backbone using jquery.ajax make restful json request. if want use other method, have override backbone.sync.
hope helpful you.
Comments
Post a Comment