javascript - Backbone not making a put request with save() after save -


i experiencing interesting problem backbone, have function in 1 of views:

addpeople :function(){         var autharray = _.clone(this.model.get("authorizedusers"));         var values = $(".add-input").val().split(",");         values.foreach(function(value) {             autharray.push(value);         });         this.model.set("authorizedusers" , autharray);         this.model.save();      }, 

this function gets called when button clicked. version of function triggers change event because cloning array, reason this.model.save()never gets called, aka server never receives put request. when refresh page go old state of model..

however if dont clone array , change function to, this:

addpeople :function(){         var autharray = this.model.get("authorizedusers");         var values = $(".add-input").val().split(",");         values.foreach(function(value) {             autharray.push(value);         });         this.model.set("authorizedusers" , autharray);         this.model.save();      }, 

this time put request sent successfully, page not re-rendered because change event not triggered. when refresh page can see updated model..

i know can manually trigger change event in second example more curious why this.model.save() not called in first example..

to understand problem more model looks like:

var postmodel = backbone.model.extend({     urlroot : '/tweet',     idattribute: '_id',     defaults:{         name: '',         comments: [],         tags: [],         authorizedusers: [],         postedby : '',         datecreated: ''     },   }); 

and node.js update function looks like:

exports.updatetweet =  function(req,res){   console.log("getting called ! ")   var update = req.body;   var id = req.url.split("/")[2];    post.update({ _id: id }, { $set: { authorizedusers: req.body.authorizedusers }}, function (err, post) {       if (err) return handleerror(err);   });   res.end();  }; 

the reason why change didn't trigger second example because same object , backbone ignore it. hence no change event triggered.

as why first 1 failed; have validator model? may validating empty string perhaps? val() can return empty string , split() on empty string return [""]

also, defaults should function otherwise model have same instance of comments, tags , authorizedusers

from backbone doc.

remember in javascript, objects passed reference, if include object default value, shared among instances. instead, define defaults function.

arrays object too.

var postmodel = backbone.model.extend({     urlroot : '/tweet',     idattribute: '_id',     defaults: function(){       return {           name: '',           comments: [],           tags: [],           authorizedusers: [],           postedby : '',           datecreated: ''       }   } }); 

lastly, array.foreach() not available on ie8 , older.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -