Sunday, 15 April 2012

Backbone.js - model.save() not firing a PUT request -


I have a basic application using Backbone.js which is not updating PUT calls (models). From the front end, I am calling a model save function does not call a put call; However, if I change it to Destroy , then it calls back-end calls. Anybody know what the problem might be? The function that does not firing the PUT request is the saveTask function.

  App.Views.Task = Backbone.View.extend ({template: _.template () "Labels & gt; ID: & lt; / labels & gt; & lt ; Input type = 'text' id = 'taskId' name = 'id' value = '<% = _id% & gt;' disabled '& gt; & lt; br & gt; & lt; label & gt; Title: & lt; / Labels & gt; & lt; input type = 'text' id = 'title' name = 'title' value = '& lt;% = title% & gt;' required /> & lt; ; Br & gt; Labels & gt; Content: & lt; / Labels & gt; & lt; input type = 'text' id = 'content' name = 'content' value = '& lt;% = content % & Gt; '/ & gt; & lt; br & gt; & lt; button ("Save input"): Click "Change", " Saving ":" saveTask "}, render: function (eventName) {$ (this.el)). Html (this.template (this.model.toJSON ())) //console.log (this.generateTemplate () ); Back to;}, change: function (event) {var target = event.target; Console.log ('By changing + target.id +': '+ target.defaultValue +' in: '+ target.value ); [Target.name] Change = target.value; this.model.set (replace); * /}, SaveTask: function () {this.model.set ({title: $ ("# title"). Val (), content: $ ("# content") Val ()}); If (this.model.isNew ()) {App.taskList.create (this.model); } And {this.model.save ({}); }}});  

If your model is new, then when you save it, then this post Method If your model is not new and you are updating it, then it will set a putte on fire.

If this is not working for you, it could be because your model does not have an ID and if you are, for example, using IDID with an example, TDID , Then in your model you have to set IDEdit in tax ID so that the spinal cord can be used as ID and all of it is normal.

Like this:

  var Task = Backbone.Model. Extension ({idAttribute: "taskId"});   

There is a link to documentation on IDTibut

There may be one more problem {} save yourself just in the call

  This.model.save (); Instead of   

  this.model.save ({});    

No comments:

Post a Comment