javascript - BackBoneJs donot Delete/Update Model to the server -
javascript - BackBoneJs donot Delete/Update Model to the server -
i have application reads/create/update model , save server. presently able read , save models , database. unable delete / update model , server. views gets deleted of model not model self here jsfiddle path http://jsfiddle.net/u17xwzlh/1/
$(function() {
model
var modelcontact = backbone.model.extend({ defaults: function() { homecoming { id: 0, name: "", address: "" }; }, //if add together idattribute = "id" deletes value server //but unable create new model/new entry database clear: function () { // deletes model changes not posted server this.destroy(); } });
collection
// runs fine var contactcollection = backbone.collection.extend({ model: modelcontact, url: 'api/contact' }); var contacts = new contactcollection;
modelview
var contactview = backbone.view.extend({ tagname: "tr", events: { // runs fine "click a.destroy": "clear" }, template: _.template($("#newcontacttemplate").html()), // runs fine initialize: function() { this.model.on("change", this.render, this); this.model.on('destroy', this.remove, this); }, render: function() { // runs fine this.$el.html(this.template(this.model.tojson())); homecoming this; }, clear: function () { this.model.clear(); } });
mainview
var main = backbone.view.extend({ el: $("#contactapp"), events: { // runs fine "click #btnsave": "createnewcontact" }, initialize: function() { // runs fine this.nameinput = this.$("#contactname"); this.addressinput = this.$("#contactaddress"); contacts.on("add", this.addcontact, this); contacts.on("reset", this.addcontacts, this); contacts.fetch(); // note : populates database values }, addcontact: function(contact) { // runs fine var view = new contactview({ model: contact }); this.$("#tblcontact tbody").append(view.render().el); }, addcontacts: function() { // runs fine contacts.each(this.addcontact); }, createnewcontact: function(e) { // runs fine contacts.create({ name: this.nameinput.val(), address: this.addressinput.val() }); } }); var m = new main;
});
right have url
defined on backbone.collection
not on backbone.model
, means have ajax work through collection
. doesn't have way though: can add together sec url on yours server-side model
ajax operations, or 2 share url (if set appropriately).
the of import part, if want able phone call this.destroy();
, have reflected on server, need:
url
property on backbone.model
set server-side url once have phone call this.destroy();
create delete ajax request, server receive request , know should delete appropriate database record, , model deleted on both client- , server-side.
javascript jquery backbone.js asp.net-web-api
Comments
Post a Comment