Skip to content

Instantly share code, notes, and snippets.

@amk221
Last active March 22, 2017 04:09
Show Gist options
  • Save amk221/a8022eac7bbe8eb52801 to your computer and use it in GitHub Desktop.
Save amk221/a8022eac7bbe8eb52801 to your computer and use it in GitHub Desktop.
Component helper didUpdateAttrs bug
import Ember from 'ember';
export default Ember.Controller.extend({
value: 1,
actions: {
changeValue() {
this.incrementProperty('value')
}
}
});
value: {{value}}<br><br>
{{#my-component my-attr=value as |api|}}
{{api.my-nested-component}}
{{/my-component}}
<br>
<button onclick={{action 'changeValue'}}>Change value</button>
<br>
<br>
<br>
<br>
import Ember from 'ember';
export default Ember.Component.extend({
classNames: ['my-component'],
didReceiveAttrs() {
console.log('my-component didReceiveAttrs with value', this.getAttr('my-attr'));
}
});
attrs.my-attr: {{attrs.my-attr}}
{{yield (hash my-nested-component=(component 'my-nested-component' my-parent-attr=attrs.my-attr))}}
{{log 'my-nested-component will receive value' attrs.my-attr}}
import Ember from 'ember';
export default Ember.Component.extend({
classNames: ['my-nested-component'],
didReceiveAttrs() {
Ember.run.scheduleOnce('afterRender', this, () => {
console.log(
'my-nested-component didReceiveAttrs with value',
this.getAttr('my-parent-attr') + ',',
'expecting',
this.$().html().match(/\d+/)[0]
);
console.log('--');
});
// This isn't working as I'd expect
this.set('myProp', this.getAttr('my-parent-attr'));
}
});
attrs.my-parent-attr: {{attrs.my-parent-attr}}<br>
myProp: {{myProp}}
body {
margin: 15px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12px;
color: gray
}
.my-component {
border: 1px solid red;
margin: 15px;
padding: 10px;
position: relative;
}
.my-component::before {
content: '{{my-component my-attr=value}}';
font-family: monospace;
position: absolute;
color: red;
top: -1.5em;
left: 0;
}
.my-nested-component {
border: 1px solid blue;
margin: 30px 10px 10px;
padding: 10px;
position: relative;
}
.my-nested-component::before {
content: '{{my-nested-component my-parent-attr=attrs.my-attr}}';
font-family: monospace;
position: absolute;
color: blue;
top: -1.5em;
left: 0;
}
{
"version": "0.4.16",
"EmberENV": {
"FEATURES": {}
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "canary",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.1.0/ember-data.js",
"ember-template-compiler": "canary"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment