Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #32 from tchak/associations-by-string

allow associations by string path bis
  • Loading branch information...
commit 1743f63afbc64dcdcdcfce29fb6acc9c0b2c228e 2 parents b74801c + f048576
@tomdale tomdale authored
View
2  packages/ember-data/lib/system/model.js
@@ -404,6 +404,8 @@ DS.hasMany = function(type, options) {
var data = get(this, 'data'), ids;
var store = get(this, 'store');
+ if (typeof type === 'string') { type = getPath(this, type); }
+
key = (options && options.key) ? options.key : key;
ids = findMany(store, type, data, key);
var hasMany = store.findMany(type, ids);
View
22 packages/ember-data/tests/associations_test.js
@@ -53,6 +53,28 @@ test("hasMany allows associations to be mapped to a user-specified key", functio
strictEqual(get(person, 'tags').objectAt(0), store.find(Tag, 2), "association objects are the same as objects retrieved directly");
});
+test("associations work when the declared with string path", function() {
+ window.App = {};
+
+ App.Person = DS.Model.extend({
+ name: DS.attr('string'),
+ tags: DS.hasMany('App.Tag')
+ });
+
+ App.Tag = DS.Model.extend({
+ name: DS.attr('string')
+ });
+
+ var store = DS.Store.create();
+ store.loadMany(App.Tag, [5, 2, 12], [{ id: 5, name: "friendly" }, { id: 2, name: "smarmy" }, { id: 12, name: "oohlala" }]);
+ store.load(App.Person, 1, { id: 1, name: "Tom Dale", tags: [5, 2] });
+
+ var person = store.find(App.Person, 1);
+ equals(get(person, 'name'), "Tom Dale", "precond - retrieves person record from store");
+
+ equals(getPath(person, 'tags.length'), 2, "the list of tags should have the correct length");
+});
+
test("associations work when the data hash has not been loaded", function() {
expect(13);
Please sign in to comment.
Something went wrong with that request. Please try again.