我收到此错误。我能够使用BackboneJs进行预形成读取和删除功能,但是当我执行add方法时,出现错误,任何帮助将不胜感激。 JSfiddel路径是http://jsfiddle.net/2wjdcgky/BackboneJS未捕获错误:必须指定“url”属性或函数
BackboneJS Uncaught Error: A "url" property or function must be specified
$(function() {
型号
var modelContact = Backbone.Model.extend({
defaults: function() {
return {
Id: 0,
Name: "",
Address: ""
};
},
idAttribute: "Id"
});
ModelCollection
var contactCollection = Backbone.Collection.extend({
model: modelContact,
url: function() {
return 'api/Contact';
},
add: function(model) {
this.sync("create", model); // Error On create
},
remove: function(model) {
this.sync("delete", model); //Runs Fine
}
});
var contacts = new contactCollection;
查看
var contactView = Backbone.View.extend({
tagName: "tr",
events: {
"click a.destroy": "clear"
},
template: _.template($("#newContacttemplate").html()),
initialize: function() {
this.model.on("change", this.render, this);
this.model.on('destroy', this.remove, this);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
},
clear: function(e) {
contacts.remove(this.model); // runs fine
}
});
主视图
var main = Backbone.View.extend({
el: $("#contactApp"),
events: {
"click #btnsave": "CreateNewContact"
},
initialize: function() {
this.Nameinput = this.$("#contactname");
this.Addressinput = this.$("#contactaddress");
contacts.on("add", this.AddContact, this);
contacts.on("reset", this.AddContacts, this);
contacts.fetch();
},
AddContact: function (contact) {
console.log("AddContact");
var view = new contactView({ model: contact });
this.$("#tblcontact tbody").append(view.render().el);
},
AddContacts: function() {
console.log("AddContacts");
contacts.each(this.AddContact);
},
CreateNewContact: function (e) {
console.log(e);
//Generate an error "BackboneJS Uncaught Error: A "url" property or function must be specified"
contacts.add({ Name: this.Nameinput.val(), Address: this.Addressinput.val() });
}
});
var m = new main;
});
你重写标准Backbone.Collections方法与“添加”,“删除”等等,这可能会导致意外的行为。最好给那个函数名称一些意思,或者直接使用标准函数名称。如果你想保存一个联系人并将其添加到集合中,请调用model.save()并将其添加到你的''成功'处理程序中的集合... – CharlieBrown 2014-10-16 13:14:30
注意点但我的删除方法工作正常,我已经覆盖它也。 – 2014-10-16 13:19:03