Skip to content

Instantly share code, notes, and snippets.

@dgeb
Created November 24, 2012 14:14
Show Gist options
  • Save dgeb/4139842 to your computer and use it in GitHub Desktop.
Save dgeb/4139842 to your computer and use it in GitHub Desktop.
hasManyEmbedded shim for ember-data
// This is a rather ugly shim that replaces the hasMany(..., {embedded: true})
// functionality that's been removed from ember-data. It is for read-only data.
// Embedded data support should eventually be added back into ember-data as an
// adapter concern.
(function() {
var get = Ember.get, set = Ember.set;
var hasEmbeddedAssociation = function(type, options) {
options = options || {};
var meta = { type: type, isAssociation: true, options: options, kind: 'hasMany' };
return Ember.computed(function(key, value) {
var data = get(this, 'data').hasMany,
store = get(this, 'store'),
ids, association;
if (typeof type === 'string') {
type = get(this, type, false) || get(window, type);
}
// as long as the embedded object data has been loaded,
// attempt to load the embedded objects into the store
if (data[key] !== undefined) {
ids = store.loadMany(type, data[key]).ids;
}
association = store.findMany(type, ids || []);
set(association, 'owner', this);
set(association, 'name', key);
return association;
// NOTE: this CP should be re-evaluated when `data` changes
}).property('data').cacheable().meta(meta);
};
DS.hasManyEmbedded = function(type, options) {
Ember.assert("The type passed to DS.hasMany must be defined", !!type);
return hasEmbeddedAssociation(type, options);
};
})();
@chrislondon
Copy link

Hey, this is awesome! Do you think you can update it to work with the current state of Ember Data? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment