Skip to content

Instantly share code, notes, and snippets.

@Samsinite
Created July 6, 2015 22:21
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 Samsinite/9feaa5f383ce6b0c11c6 to your computer and use it in GitHub Desktop.
Save Samsinite/9feaa5f383ce6b0c11c6 to your computer and use it in GitHub Desktop.
Ember Data nested join relation computed function
import { createSortableArray } from './sortable-array-proxy';
const { get, set } = Ember;
/****
* TODO: If need sorting, add sort logic
*****/
export default function computedManyThroughAsync(joinRelationAttr, nestedRelationAttr, sortProperties, sortAscending = true) {
return Ember.computed(joinRelationAttr, function() {
var joinRelationPromise = get(this, joinRelationAttr);
if (joinRelationPromise) {
return DS.PromiseArray.create({
promise: joinRelationPromise.then(function(joinModels) {
if (joinModels) {
var promiseArray = [];
joinModels.forEach(function(joinModel) {
var nestedRelationPromise = get(joinModel, nestedRelationAttr);
if (nestedRelationPromise) {
promiseArray.push(get(nestedRelationPromise));
}
});
return Ember.RSVP.all(promiseArray);
});
});
}
});
}
import DS from 'ember-data';
import computedManyThroughAsync from '../utils/computed-many-through-async'; /* say ^^^ is stored here */
/*******
* say a join model exists called 'foo-bar' like so
* export default DS.Model.extend({
* bar: DS.belongsTo('bar'),
* foo: DS.belongsTo('foo')
* });
*******/
export default DS.Model.extend({
fooBars: DS.hasMany('foo-bar'),
bars: computedManyThroughAsync('fooBars', 'foo')
});
/* Now foo.get('bars') returns a PromiseArray of nested bar models. */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment