Skip to content

Commit

Permalink
Adds ability to introspect association types
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Dale committed Jan 26, 2012
1 parent fefcf48 commit f7fc290
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
16 changes: 14 additions & 2 deletions packages/ember-data/lib/system/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,16 @@ DS.Model = Ember.Object.extend({
}
});

DS.Model.reopenClass({
typeForAssociation: function(association) {
var type = this.metaForProperty(association).type;
if (typeof type === 'string') {
type = getPath(this, type, false) || getPath(window, type);
}
return type;
}
});

DS.attr = function(type, options) {
var transform = DS.attr.transforms[type];
var transformFrom = transform.from;
Expand Down Expand Up @@ -405,7 +415,9 @@ var hasAssociation = function(type, options, one) {
var data = get(this, 'data'), ids, id, association,
store = get(this, 'store');

if (typeof type === 'string') { type = getPath(this, type); }
if (typeof type === 'string') {
type = getPath(this, type, false) || getPath(window, type);
}

key = (options && options.key) ? options.key : key;
if (one) {
Expand All @@ -417,7 +429,7 @@ var hasAssociation = function(type, options, one) {
}

return association;
}).property('data').cacheable();
}).property('data').cacheable().meta({ type: type });
};

DS.hasMany = function(type, options) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-data/lib/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ DS.Store = Ember.Object.extend({
_adapter: Ember.computed(function() {
var adapter = get(this, 'adapter');
if (typeof adapter === 'string') {
return getPath(this, adapter);
return getPath(this, adapter, false) || getPath(window, adapter);
}
return adapter;
}).property('adapter').cacheable(),
Expand Down
52 changes: 52 additions & 0 deletions packages/ember-data/tests/associations_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,58 @@ test("hasMany lazily loads associations as needed", function() {
strictEqual(get(wycats, 'tags').objectAt(0), store.find(Tag, 12), "association objects are the same as objects retrieved directly");
});

test("should be able to retrieve the type for a hasMany association from its metadata", function() {
var Tag = DS.Model.extend({
name: DS.attr('string')
});

var Person = DS.Model.extend({
name: DS.attr('string'),
tags: DS.hasMany(Tag)
});

equal(Person.typeForAssociation('tags'), Tag, "returns the association type");
});

test("should be able to retrieve the type for a hasMany association specified using a string from its metadata", function() {
window.Tag = DS.Model.extend({
name: DS.attr('string')
});

var Person = DS.Model.extend({
name: DS.attr('string'),
tags: DS.hasMany('Tag')
});

equal(Person.typeForAssociation('tags'), Tag, "returns the association type");
});

test("should be able to retrieve the type for a hasOne association from its metadata", function() {
var Tag = DS.Model.extend({
name: DS.attr('string')
});

var Person = DS.Model.extend({
name: DS.attr('string'),
tags: DS.hasOne(Tag)
});

equal(Person.typeForAssociation('tags'), Tag, "returns the association type");
});

test("should be able to retrieve the type for a hasOne association specified using a string from its metadata", function() {
window.Tag = DS.Model.extend({
name: DS.attr('string')
});

var Person = DS.Model.extend({
name: DS.attr('string'),
tags: DS.hasOne('Tag')
});

equal(Person.typeForAssociation('tags'), Tag, "returns the association type");
});

test("hasMany allows associations to be mapped to a user-specified key", function() {
var Tag = DS.Model.extend({
name: DS.attr('string')
Expand Down

0 comments on commit f7fc290

Please sign in to comment.