我收到此錯誤。我能夠使用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