Skip to content

Instantly share code, notes, and snippets.

@Panman82
Last active February 10, 2016 16:57
Show Gist options
  • Save Panman82/f1740177799321f88eda to your computer and use it in GitHub Desktop.
Save Panman82/f1740177799321f88eda to your computer and use it in GitHub Desktop.
didReceiveAttrs() hook missed
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle',
init() {
this._super( ...arguments );
this.set('controllerHash', Ember.Object.create({
foo: 'Foo'
}));
this.set('hashStateLog', Ember.A());
this.logHashState(); // for initial state
}, // init()
logHashState() {
this.get('hashStateLog').pushObject(
JSON.stringify(
this.get('controllerHash')
)
);
}, // logHashState()
actions: {
registerFoobar( component ) {
this.set('foobar', component);
}, // registerFoobar()
setFoo() {
let controllerHash = this.get('controllerHash');
controllerHash.foo = 'Boo';
this.set('controllerHash', controllerHash);
this.logHashState();
this.get('foobar').send('logHashState');
}, // setFoo()
setFooToZoo() {
this.set('controllerHash.foo', 'Zoo');
this.logHashState();
this.get('foobar').send('logHashState');
}, // setFooToZoo()
setBazToBaz() {
this.set('controllerHash.baz', 'Baz');
this.logHashState();
this.get('foobar').send('logHashState');
}, // setBazToBaz()
setBazToZaz() {
this.set('controllerHash.baz', 'Zaz');
this.notifyPropertyChange('controllerHash');
this.logHashState();
this.get('foobar').send('logHashState');
} // setBazToZaz()
} // :actions
});
<h1>Welcome to {{appName}}</h1>
<p>Issue, when changing a bound object / hash property, the Components <code>didReceiveAttrs()</code> is never triggered even though the propery change makes it to the Components "copy".</p>
{{my-foobar componentHash=controllerHash}}
<button {{action "setFoo"}}>
this.set('controllerHash', controllerHash) // foo = 'Boo'
</button>
I'd expect this to work, but doesn't...
<br>
<button {{action "setFooToZoo"}}>
this.set('controllerHash.foo', 'Zoo')
</button>
Doesn't work...
<br>
<button {{action "setBazToBaz"}}>
this.set('controllerHash.baz', 'Baz')
</button>
Doesn't work, but new property added...
<br>
<button {{action "setBazToZaz"}}>
this.set('controllerHash.baz', 'Zaz');
this.notifyPropertyChange('controllerHash')
</button>
Works, because we specifically told observers to fire
<br>
<br>
<br>
<table style="width:100%">
<thead><tr>
<th><code>controllerHash:{}</code> State Log</th>
<th><code>didReceiveAttrs()</code> Log</th>
<th><code>componentHash:{}</code> State Log</th>
</tr></thead>
<tbody><tr>
<td style="vertical-align:top"><ol>
{{#each hashStateLog as |entry|}}
<li>{{entry}}</li>
{{/each}}
</ol></td>
<td style="vertical-align:top"><ol>
{{#each foobar.attrHookLog as |entry|}}
<li>{{entry}}</li>
{{/each}}
</ol></td>
<td style="vertical-align:top"><ol>
{{#each foobar.hashStateLog as |entry|}}
<li>{{entry}}</li>
{{/each}}
</ol></td>
</tr></tbody>
</table>
import Ember from 'ember';
export default Ember.Component.extend({
registerFoobar: 'registerFoobar',
componentHash: {},
init() {
this._super( ...arguments );
this.set('attrHookLog', Ember.A());
this.set('hashStateLog', Ember.A());
this.sendAction('registerFoobar', this);
this.send('logHashState');
}, // init()
didReceiveAttrs() {
this.get('attrHookLog').pushObject(
JSON.stringify(
this.get('componentHash')
)
);
}, // didReceiveAttrs()
actions: {
logHashState() {
this.get('hashStateLog').pushObject(
JSON.stringify(
this.get('componentHash')
)
);
} // logHashState()
} // :actions
});
<h2 style="margin-bottom:0">Foobar Component</h2>
<p style="margin:0 0 25px"><code>&#123;&#123;my-foobar componentHash=controllerHash&#125;&#125;</code></p>
{
"version": "0.5.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.2.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment