Is there a way to change the observed value without triggering an observer callback in Polymer

As the name says. Is there a way to change observed values ​​without triggering an observer callback in Polymer?

for example

Polymer({ is: 'my-component', properties: { aValue: { type: Number, value: 0, observer: '_valueChanged', notify: true }, ref: { type: Object, computed: '_computeRef(channel, channelNumber)' } }, _computeRef: function(channel, channelNumber) { var ref = new Firebase("/*link*/"); ref.on("child_changed", function(data) { this.aValue.setWithoutCallingObserver(data.val()); }.bind(this)); return ref; }, _valueChanged: function() { var message = { aValue: this.aValue }; if (this.ref) { this.ref.set(message); } } }); 

This would be useful, because now I am lagging in the following scenario:

  • Adapting aValue in a third-party application
  • Firebase updates all clients
  • .On callback sets value and calls observer callback
  • Calls .set for firebase
  • return to 2.

Update: the problem is not related to firebase. I believe the solution is to gain control over how updates apply to observed values ​​in Polymer. Partly because changes to the values ​​in the firebase store can also be done by 3rd party (not necessarily web applications).

+7
polymer object.observe
source share
2 answers

As far as I know, there is no built-in way to set the value of a property without starting its observer.

You do not have control over how / when / with which argument the observer is called, but you have control over the body of the procedure and, fortunately, you work with the general state ( this ).

So, you can change the behavior of the function based on the flag, which can be accessed from within the function, but does not need to be passed.

For example:

 _valueChanged: function (new_val, old_val) { if (this._observerLock) { return; } var message = { aValue: this.aValue }; if (this.ref) { this.ref.set(message); } } }, ... 

Then you can implement the _setWithoutCallingObserver() method, for example:

 _setWithoutCallingObserver: function (value) { this._observerLock = true; this.aValue = value; this._observerLock = false; } 
+4
source share

Just change _valueChangedMethod to this

 _valueChanged: function(newValue, oldValue) { if(newValue == oldValue) return; var message = { aValue: this.aValue }; if (this.ref) { this.ref.set(message); } } 

This will force the observer to do his work only if the value has really changed.

-one
source share

All Articles