javascript-如何建立骨干模型,并且它的视图没有定义集合?当前有空的请求有效负载
作者:互联网
我以为我对Model,ModelView,Collection,AppView sample on github/documentcloud/backbone有一定的了解,直到..
我试图设置一些更精简的东西.我正在尝试只具有一个模型和一个视图.
我的问题
The post of data does not work; the request payload in the network trace is empty, what have I missed?
我在jsFiddle here(A)上有完整的代码.我还测试了适用于我的环境的示例代码也适用于on jsFiddle(B),并且网络检查显示请求有效负载中包含数据.
模型和查看代码
window.MyModel = Backbone.Model.extend({
urlRoot: '/api/m',
});
window.MView = Backbone.View.extend({
template: _.template($('#template').html()),
el: $('#modelfields'),
initialize: function () { this.model.bind('change', this.render, this); },
events: {
"click .save": function () { this.model.save(); }
},
render: function () {
$(this.el).html(this.template(this.model.toJSON()));
return this;
},
});
window.MyMView = new MView({model: new MyModel});
网络窗口屏幕截图
>(A)Response data empty on my sample
>(B)The official demo works fine
解决方法:
您的模型没有属性!设置一些默认值或调用this.model.set({..})来添加它们.
我已经更新了您的示例,使其包括默认值和表单中的字段.
window.MyModel = Backbone.Model.extend({
defaults: {
'defaultvalue':'somedefault'
},
urlRoot: '/api/m',
});
window.MView = Backbone.View.extend({
template: _.template($('#template').html()),
el: $('#modelfields'),
initialize: function () { this.model.bind('change', this.render, this); },
events: {
"click .save": 'save'
},
save: function() {
var description = this.$("#Description").val();
var moreData = this.$("#MoreData").val();
this.model.set({"description":description,"more":moreData});
this.model.save();
},
render: function () {
$(this.el).html(this.template(this.model.toJSON()));
return this;
},
});
window.MyMView = new MView({model: new MyModel});
标签:backbone-js,javascript 来源: https://codeday.me/bug/20191201/2082308.html