backbone.js - backbone model toJSON strangely cache itself -


I am still new to the spinal cord:

My problem is that it is difficult to explain Model: {ID: xxx, question: [id: "yy", select: [{option id: "aaa"}, ..., {optionId: / p>

 "Zzz"}}}, ....]}   

An event method update selection is archived. After the event is triggered, I get two different results with two codes below:

  window.pkg.Questions.get (this.Id). SelectSetset (selected option); Console.log (window.pkg.Questions.get (this.Id) .Selections.toJSON ()); Console.log (window.pkg.Questions.get (this.Id) .toJSON () selection.);   

The first log shows the updated model, though it later shows the initial default value.

Why is this working?

Enter image details here

< Div class = "post-text" itemprop = "text">

These are two separate copies: There is a selection in your question model, which is called a property called a selection, and a backbone feature, also called selection is. I think that you want to use the selection collection, and will not be in sync with that feature is the original Jason which I think was inadvertently added to the model.

It's hard to say to fix this, because you have not shown a code where you create these models and collect them. Wherever you pick JSON in the selection and initially put reset in the archive, you can remove it from the JSON and / or unset question already on the questionnaire. ...

This will also print incorrect, original data:

  console.log (window.pkg.Questions.get (this.Id) .get ('selection ')); You can remove it in QuestionModel's  parse :  
  Pars: function (data) {// print> original JSON   

// The selection from data will prevent you from adding // as a feature here. Delete data. Selection; Return data; }

If you do this, and you want the question to be included in question model's toJSON , then you override toJSON .

  toJSON: function () {// get JSON for question var var json = Backbone.Model.prototype.toJSON.call (this); // Update json to json for selection Selection = this. Selection. Taso (); Return json; }    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -