ember-computed-indirect icon indicating copy to clipboard operation
ember-computed-indirect copied to clipboard

Issue with computedIndirect and two-way bindings

Open fengb opened this issue 9 years ago • 1 comments

When using two-way bindings, a computedIndirect will fire multiple observers:

import Ember from 'ember'
import computedIndirect from 'ember-computed-indirect/utils/indirect'

let Buggy = Ember.Object.extend({
  valuePath: Ember.computed(function () {
    return '_underlying'
  }),

  value: computedIndirect('valuePath'),
  bindedValueBinding: 'value',

  valueObserver: Ember.observer('value', function () {
    console.log('Observe! ' + this.get('value'))
  })
})

> buggy = Buggy.create()
< Object([...])
> buggy.set('bindedValue', 1) // fires two events
< 1
  Observe! 1
  Observe! 1
> buggy.set('value', 2) // fires two events with 1 receiving the wrong value
  Observe! 1
  Observe! 2
< 2

Since components uses bindings, I cannot just convert them into Ember.computed.alias.

fengb avatar Dec 01 '15 21:12 fengb

Yeah, this is very peculiar. It seems it's happening because the setting of value and the setting of _underlying aren't happening atomically. I think we might I might be able to fix that by pausing change observers while modifying the underlying value, but I'm not sure. I'll noodle over this a bit and try to come up with a solution.

gordonkristan avatar Dec 16 '15 03:12 gordonkristan