Saturday 15 February 2014

javascript - Backbone Model .changedAttributes() not showing all changes -


My short model looks like this:

  var model = new backbone.model ( {Default: {x: 50, y: 50, constrain_proportions: true}, initialize: function () {// Accessories to calculate the aspect ratio of X and Y ('Change: x', doStuff, this); ('Change: y', dustf, this);}, dustf: function () {// ... if (this ('constrain_proportions')) {var changes = this.changedAttributes (); / / Make sure to do the stuff to limit the ratio}}});   

I am running on an issue where I am making such a change:

  model.set ({x: 50, y: 60} );   

In my doStuff method, I have to make sure that when constrain_proportions is set to true, changing an attribute, Keeping the other one, keeping the same aspect ratio when I update x and y together, the aspect ratio changes in the problem that I'm running into Here I am having that when you make any changes to the backbone model using the code above, then the x attribute diff The value is similar to the backbone, for this reason model.changedAttributes () to return:

  {y: 60}   

The reason is this part of the code in the Model.set method:

  // For each `set` attribute, update or delete the current value . For attr in attrs {val = attrs [attr]; If (! _eikal (current [atri], val) varies. Pish (atri); if (! _value (former [atri], val)) {this.changed [attr] = val;} Else {this Delete.rateed [attr]; // offender right here) unset? Delete current [atri]: current [atr] = val;}   

x value is changed from y to 60 and further changed to 50, my code updates x to value 60, so that He keeps the initial aspect of the model with the aspect ratio of 1: 1. {x: 50, y: 60} de I want to change the aspect ratio to 5: 6, but this change occurs in the code above the backbone when the value is being changed

How do I get it successfully? ?

When I wanted to emphasize the occurrence of a change, I set up the feature quietly Reset time:

  model.unset ('x', {silent: true}). Unset ('y', {silent: true}) .set ({x: 50, y: 60});   

To make it more convenient, you can wrap it in another function on the model:

  setXY: function (x, y) { This.unset ('x', (silent: true)). Set ('y', {silence: true}). Set ({x: x, y: y}); }    

No comments:

Post a Comment