Skip to content

Instantly share code, notes, and snippets.

@alexgb
Last active May 9, 2016 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexgb/4199d0eef326a5d22ac26f446dc710de to your computer and use it in GitHub Desktop.
Save alexgb/4199d0eef326a5d22ac26f446dc710de to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
import DS from 'ember-data';
function delayedResolveWith(obj) {
return new Ember.RSVP.Promise(function(resolve, reject) {
window.setTimeout(resolve.bind(null, obj), 1000);
});
}
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
items: null,
init() {
this._super(...arguments);
Ember.set(this, 'items', [{
nested : DS.PromiseObject.create({
promise: delayedResolveWith({ id: '123' })
}),
nestedId: Ember.computed.reads('nested.id')
}, {
nested : DS.PromiseObject.create({
promise: delayedResolveWith({ id: '234' })
}),
nestedId: Ember.computed.reads('nested.id')
}, {
nested : DS.PromiseObject.create({
promise: delayedResolveWith({ id: '345' })
})
}]);
},
itemsWithNestedId: Ember.computed('items.@each.nested', function() {
return Ember.get(this, 'items').map((item) => {
return {
nested: Ember.get(item, 'nested'),
nestedId: Ember.computed.reads('nested.id')
};
});
}),
allIds: Ember.computed('itemsWithNestedId.@each.nestedId', function() {
return Ember.get(this, 'itemsWithNestedId').mapBy('nestedId').join(', ');
})
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<div>
<strong>items:</strong>
<ul>
{{#each items as |item|}}
<li>{{item.nested.id}}</li>
{{/each}}
</ul>
</div>
<div>
<strong>all item ids:</strong> {{allIds}}
</div>
<br>
<br>
{
"version": "0.8.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.5.1",
"ember-data": "2.5.2",
"ember-template-compiler": "2.5.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment