首次尝试使用骨干模型集合将数据从骨干发布到REST。
define(["underscore" , "backbone"],function(_ , Backbone){
var CustomerModel = Backbone.Model.extend({
urlRoot: 'http://myresturl/api/CusWeb',
initialize: function(){
},
defaults : {
UID: "00000000-0000-0000-0000-000000000000",
Sex: 0,
Name: "",
Company: "",
Address: "",
Email: ""
}
});
return CustomerModel;
});
define(["underscore","backbone","models/CustomerModel"] , function(_ ,Backbone,CustomerModel){
var CustomerCollection = Backbone.Collection.extend({
url: 'http://myresturl/api/CusWeb',
model:CustomerModel
});
return CustomerCollection;
});
我正在尝试通过单击骨干视图中的按钮将数据从骨干网发布到REST URL。
define(["jquery" ,
"underscore" ,
"backbone" ,
"text!templates/Customer/registerCustomerTemplate.html",
"models/CustomerModel",
"user",
],function($ , _ , Backbone, RegisterDescriptionTem, CustomerModel, Customer){
var userObj = new Customer();
var ContainerRegisterView = Backbone.View.extend({
el : $("#webbodycontainer"),
initialize : function(){
},
events : {
'click #register' : 'registerUser'
},
registerUser : function(){
this.model.set({
UID: "00000000-0000-0000-0000-000000000000",
Sex: 1,
Name: "",
Company: "",
Address: "",
Email: ""
});
this.model.save();
console.log(this.model.get('Sex'));
},
render : function(){
var _registerDes = _.template(RegisterDescriptionTem);
this.model = new CustomerModel();
this.$el.append(_registerDes((this.model.toJSON())));
}
});
return ContainerRegisterView;
});
console.log(this.model.get('Sex'));
显示在浏览器的控制台(1)中,但数据未保存到数据库。
这是路由器:
define([
.....
'views/Customer/ContainerRegister',
], function(ContainerRegister){
var AppRouter = Backbone.Router.extend({
routes: {
......
}
var app_router = new AppRouter;
app_router.on('route:registerAction', function(register){
if(window.currentSection){
window.currentSection.remove();
}
window.currentSection = new ContainerRegister({});
$('#webbodycontainer').html(window.currentSection.$el);
window.currentSection.render(register);
});
});
我感谢您提供的任何其他信息。感谢。
答案 0 :(得分:3)
检查初始化模型。并且在保存时尝试将第一个参数设为null。
define(["jquery" ,
"underscore" ,
"backbone" ,
"text!templates/Customer/registerCustomerTemplate.html",
"models/CustomerModel",
"user",
],function($ , _ , Backbone, RegisterDescriptionTem, CustomerModel, Customer){
var ContainerRegisterView = Backbone.View.extend({
el : $("#webbodycontainer"),
//check initializing your model here.
initialize : function(){
_.bindAll(this);
this.model = new Customer();
},
events : {
'click #register' : 'registerUser'
},
registerUser : function(){
this.model.set({
UID: "00000000-0000-0000-0000-000000000000",
Sex: 1,
Name: "",
Company: "",
Address: "",
Email: ""
});
this.model.save(null,{
success:function(){
//code after success
},error:function(){
//code after error
},
});
console.log(this.model.get('Sex'));
},
render : function(){
var _registerDes = _.template(RegisterDescriptionTem);
this.model = new CustomerModel();
this.$el.append(_registerDes((this.model.toJSON())));
}
});
return ContainerRegisterView;
});
答案 1 :(得分:0)
我认为您的问题可能是缺少模型id
。对于主干< - >服务器交互,您可能会发现需要id
属性。尝试按如下方式修改代码:
define(["underscore" , "backbone"],function(_ , Backbone){
var CustomerModel = Backbone.Model.extend({
urlRoot: 'http://myresturl/api/CusWeb',
initialize: function(){
},
defaults : {
UID: "00000000-0000-0000-0000-000000000000",
Sex: 0,
Name: "",
Company: "",
Address: "",
Email: ""
},
idAttribute: "UID" // set "UID" as the alias for "id"
});
return CustomerModel;
});
这是一篇关于类似问题的博文,内容更为详细http://blog.markstarkman.com/blog/2012/02/13/getting-backbone-dot-js-to-sync-with-rails-and-mongoid/
答案 2 :(得分:0)
首先,你需要在模型上设置idAttribute,如@SteamDev所说。
var CustomerModel = Backbone.Model.extend({
urlRoot: 'http://myresturl/api/CusWeb',
initialize: function(){
},
defaults : {
//UID: "00000000-0000-0000-0000-000000000000",
Sex: 0,
Name: "",
Company: "",
Address: "",
Email: ""
},
idAttribute: "UID" // set "UID" as the alias for "id"
});
其次,您不需要设置UID值(既不是模型的默认值,也不是模型实例的默认值)。