Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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