Created
July 6, 2015 22:21
-
-
Save Samsinite/9feaa5f383ce6b0c11c6 to your computer and use it in GitHub Desktop.
Ember Data nested join relation computed function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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