Thursday 15 July 2010

javascript - BackboneJS Uncaught Error: A "url" property or function must be specified -



javascript - BackboneJS Uncaught Error: A "url" property or function must be specified -

i getting error . able preform read, , remove functions using backbonejs , having error when execute add together method help appreciated. jsfiddel path http://jsfiddle.net/2wjdcgky/

backbonejs uncaught error: "url" property or function must specified $(function() {

model

var modelcontact = backbone.model.extend({ defaults: function() { homecoming { id: 0, name: "", address: "" }; }, idattribute: "id" });

modelcollection

var contactcollection = backbone.collection.extend({ model: modelcontact, url: function() { homecoming '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;

view

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())); homecoming this; }, clear: function(e) { contacts.remove(this.model); // runs fine } });

main view

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 error "backbonejs uncaught error: "url" property or function must specified" contacts.add({ name: this.nameinput.val(), address: this.addressinput.val() }); } }); var m = new main;

});

your jsfiddle missing backbone references , all.

working update: http://jsfiddle.net/apt7hchl/2/

much simpler code (no need define add , remove methods on collection!). more mutual javascript coding style conventions.

please note had manually generate "id" attribute allow creating more 1 contact. making id = 0 default, sec model same not added, backbone sees model id=0 in collection.

when want save, phone call model.save() method. don't phone call sync manually, you'll don't need to!

for model saved database before beingness added collection, use:

createnewcontact: function (e) { e.preventdefault(); var self = this; var newcontact = new contactmodel({ name: this.$("#name").val(), address: this.$("#address").val() }); newcontact.save({ success: function(model){ self.collection.add(model); }); //clear form this.$("#name").val(""); this.$("#address").val("");

}

javascript jquery backbone.js asp.net-web-api

No comments:

Post a Comment