Skip to content

Instantly share code, notes, and snippets.

@dio-el-claire
Created June 19, 2014 09:00
Show Gist options
  • Save dio-el-claire/7217f877c866b41d97b7 to your computer and use it in GitHub Desktop.
Save dio-el-claire/7217f877c866b41d97b7 to your computer and use it in GitHub Desktop.
var get = Ember.get;
Ember.Model.reopen({
getBelongsTo: function(key, type, meta, store) {
var idOrAttrs = get(this, '_data.' + key),
record;
if (Ember.isNone(idOrAttrs)) {
return null;
}
if (meta.options.embedded) {
var primaryKey = get(type, 'primaryKey'),
id = idOrAttrs[primaryKey];
record = type.create({ isLoaded: false, id: id, container: this.container });
record.load(id, idOrAttrs);
} else {
if (store) {
record = store.find(meta.type, idOrAttrs, false);
} else {
record = type.find(idOrAttrs);
}
}
return record;
}
});
Ember.Model.Store.reopen({
find: function(type, id, isFetch) {
var klass = this.modelFor(type),
isFetch = isFetch === void 0 ? true : !!isFetch;
klass.reopenClass({adapter: this.adapterFor(type)});
if (!id) {
return klass._findFetchAll(isFetch, this.container);
} else if (Ember.isArray(id)) {
return klass._findFetchMany(id, isFetch, this.container);
}else if (typeof id === 'object') {
return klass._findFetchQuery(id, isFetch, this.container);
} else {
return klass._findFetchById(id, isFetch, this.container);
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment