Created
November 24, 2012 14:14
-
-
Save dgeb/4139842 to your computer and use it in GitHub Desktop.
hasManyEmbedded shim for ember-data
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
// 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); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, this is awesome! Do you think you can update it to work with the current state of Ember Data? Thanks!