问题
I am working on a project where I use Polymer in combination with Firebase. I have a knob element that is just a knob which value is stored in Firebase. The value of the knob is observed by a polymer observer, that when the value changes updates the firebase value.
The problem is the following : when the value is changed in one place it updates the firebase value. This emits a change event to all other places. In other places this makes the value in the element being set and hence the observer triggered. This observer causes the firebase value being set again. This in turn emits again a change, and so forth ... This causes laggy behaviour when adapting the knob. What can I do?
<script>
Polymer({
is: 'disco-ccontrol',
properties: {
midiValue: {
type: Number,
value: 0,
observer: '_valueChanged',
notify: true
},
channel: {
type: Number,
value: 0
},
channelNumber: {
type: Number,
value: 0
},
ref: {
type: Object,
computed: '_computeRef(channel, channelNumber)'
}
},
_computeRef: function(channel, channelNumber) {
var ref = new Firebase("https://incandescent-inferno-8405.firebaseio.com/user/"+this.channel+'/'+this.channelNumber);
ref.on("child_changed", function(data) {
this.midiValue = data.val();
}.bind(this));
return ref;
},
_valueChanged: function() {
var message = { value: this.midiValue, channel: this.channel, channelNumber: this.channelNumber };
if (this.ref) {
this.ref.set(message);
}
}
});
</script>
回答1:
How about instead of
ref.on("child_changed", function(data) {
you use
ref.once("child_changed", function(data) {
to only get the value from firebase at startup & after that you are only updating the value in firebase.
Of course depends on your overall app architecture, but from what you show here it seems ok.
来源:https://stackoverflow.com/questions/32529499/firebase-syncing-causes-lag