Search code examples
ember.jsember-cli

Component resets all attributes when a dynamic attribute is changed


I have a component that I pass some static and dynamic parameters. The component updates these static values during interaction. When dynamic parameters are updated by the implementing component, all the static parameters are reset. I would expect that the component retains the updated value.

Ember Twiddle

I have implemented a work around where those values are set only during init, but I am wondering if this is intended behaviour.


Solution

  • In the ember-twiddle example… showYield=false the static value false is changed via an action after render. Then the update time action changes time in the parent context (controller) causing the component template to redraw (which seems to have the side effect of resetting the showYield back to false.)

    Since time is changed and time is passed to component the component re-renders using false, even after showYield was set to true from an action handler. {{#outer-component time=time showYield=false}}

    So initially showYield is false, when passed into the component, the component has an action which changes showYield in the context of the component. false is always false, it's static. So next time is changed on the outer context (that is passed into the component) and that is (re)passed back into he component template. Since the static value is also (re)passed back into the component it's effectively reset.

    I don't think you want to mix the static value and dynamic values of the component. Perhaps define the default value for showYield as false in a property; and use that property instead of the static value showYield=property.