Observer in component not triggered by injected property change


#1

Hi there,

I’ve a problem building a trigger, reset relationship between two components.

  • Parent trigger action by toggling property, child component detect the change and send an action back to the parent which reset its state.

The output is the following:

"DEBUG: parent trigger"
"DEBUG: parent: observer triggered"
"DEBUG: parent set true"
"DEBUG: child: observer triggered true"
"DEBUG: parent reset"
"DEBUG: parent: observer triggered"
"DEBUG: parent set false"

I thought observer were synchronous, and that the results would be “set, parent obs, child obs, ember.debug”, but it’s not, the child obs is called after the trigger method

"DEBUG: parent trigger"
"DEBUG: parent: observer triggered"
"DEBUG: child: observer triggered true"
"DEBUG: parent set true"

And over that, child obs is never called after reset, which result the child property stays ‘true’ and won’t trigger reset anymore.

Can you give me a hint on what’s wrong with this design?

Thanks a lot,


#2

Changes are not synchronized synchronously. They’re scheduled on a queue (i.e., the set).

I recommend checking out the documentation on the runloop to better understand it.

https://guides.emberjs.com/v2.5.0/applications/run-loop/

That said, there are probably ways to achieve what you’re after without the use of observers. I can’t really tell what that is because your example is a bit contrived.

Another great talk on observers by Stefan worth a look to see why you might want to explore an alternative approach: https://www.youtube.com/watch?v=7PUX27RKCq0


#3

Thanks for these links, they will be useful for sure.

My real question is. Why in the first case, the property change does trigger the observer of the child component, and when it’s changed again (to false) it is not?


#4

#5

Thanks @Photon79

In your example the child observer is triggered twice each time. What is the reason of that?

– Leo


#6

I don’t know :slight_smile:

Fixed example: http://emberjs.jsbin.com/wodiduvejo/edit?html,js,console,output


#7

Ok all right, I’d be curious to know. But thanks, it works as expected :slight_smile: