Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions packages/ember-model/lib/belongs_to.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var get = Ember.get,
set = Ember.set;
set = Ember.set;

function storeFor(record) {
if (record.container) {
Expand All @@ -9,20 +9,45 @@ function storeFor(record) {
return null;
}

function isValidType(type, record) {
var hasContainer = record.container;
var isObject = (typeof type === "object" || typeof type === "function");

if (hasContainer && isObject) {
return false;
}

if (!isObject) {
if (hasContainer && Ember.get(Ember.lookup, type)) {
return false;
}
}

return true;
}

function getType(record) {
var type = this.type;

if (typeof this.type === "string" && this.type) {
type = Ember.get(Ember.lookup, this.type);
if (record) {
Ember.assert("Models created from store must define relationships with strings not objects. Using convention 'post' not 'App.Post'", isValidType(this.type, record));
}
if (typeof this.type === "object" || typeof this.type === "function") {
return this.type;
}

this.type = get(Ember.lookup, type);

if (!type) {
var store = storeFor(record);
type = store.modelFor(this.type);
type.reopenClass({ adapter: store.adapterFor(this.type) });
}
if (this.type) {
return this.type;
}

return type;
else {
var store = storeFor(record);
this.type = store.modelFor(type);
this.type.reopenClass({ adapter: store.adapterFor(type) });
return this.type;
}
}

Ember.belongsTo = function(type, options) {
Expand Down
40 changes: 32 additions & 8 deletions packages/ember-model/lib/has_many.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
var get = Ember.get;

function isValidType(type, record) {
var hasContainer = record.container;
var isObject = (typeof type === "object" || typeof type === "function");

if (hasContainer && isObject) {
return false;
}

if (!isObject) {
if (hasContainer && get(Ember.lookup, type)) {
return false;
}
}

return true;
}

function getType(record) {
var type = this.type;

if (typeof this.type === "string" && this.type) {
this.type = Ember.get(Ember.lookup, this.type);
Ember.assert("Models created from store must define relationships with strings not objects. Using convention 'post' not 'App.Post'", isValidType(this.type, record));

if (!this.type) {
var store = record.container.lookup('store:main');
this.type = store.modelFor(type);
this.type.reopenClass({ adapter: store.adapterFor(type) });
}
if (typeof this.type === "object" || typeof this.type === "function") {
return this.type;
}

this.type = Ember.get(Ember.lookup, type);

if (this.type) {
return this.type;
}

return this.type;
else {
var store = record.container.lookup('store:main');
this.type = store.modelFor(type);
this.type.reopenClass({ adapter: store.adapterFor(type) });
return this.type;
}
}

Ember.hasMany = function(type, options) {
Expand Down
6 changes: 5 additions & 1 deletion packages/ember-model/lib/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ Ember.Model.Store = Ember.Object.extend({
container: null,

modelFor: function(type) {
return this.container.lookupFactory('model:'+type);
if (typeof type === 'string') {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sanity check

return this.container.lookupFactory('model:'+type);
} else {
return type;
}
},

adapterFor: function(type) {
Expand Down
23 changes: 23 additions & 0 deletions packages/ember-model/tests/belongs_to_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,4 +725,27 @@ test("non embedded belongsTo should return a record with a container", function(
start();
ok(article.get('container'));
});
});

test("model cannot be specified as an object if container exists", function() {
var App;
Ember.run(function() {
App = Ember.Application.create({});
});
App.Article = Ember.Model.extend({
id: Ember.attr(String)
});
App.Comment = Ember.Model.extend({
article: Ember.belongsTo(App.Article, { key: 'article', embedded: true })
});

var comment = App.Comment.create({container: App.__container__});
Ember.run(comment, comment.load, 1, { article: { id: 'a' } });

expectAssertion(function() {
comment.get('article');
},
/Models created from store must define relationships with strings not objects. Using convention 'post' not 'App.Post'/);

Ember.run(App, 'destroy');
});
29 changes: 27 additions & 2 deletions packages/ember-model/tests/has_many_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ test("model can be specified with a string to a resolved path", function() {
});
App.Comment = Ember.Model.extend({
id: Ember.attr(String),
subComments: Ember.hasMany('subcomment', { key: 'subcomments', embedded: true })
subComments: Ember.hasMany('subcomment', {embedded: true })
});
App.Article = Ember.Model.extend({
comments: Ember.hasMany('comment', { key: 'comments', embedded: true })
comments: Ember.hasMany('comment', {embedded: true })
});

var article = App.Article.create({container: App.__container__});
Expand Down Expand Up @@ -275,4 +275,29 @@ test("key defaults to model's property key", function() {
Ember.run(article, article.load, 1, { comments: Ember.A(['a'] )});

deepEqual(article.toJSON(), { comments: ['a'] });
});

test("model cannot be specified as an object if container exists", function() {
var App;
Ember.run(function() {
App = Ember.Application.create({});
});

App.Comment = Ember.Model.extend({
id: Ember.attr()
});
App.Article = Ember.Model.extend({
comments: Ember.hasMany(App.Comment, {embedded: true })
});

var article = App.Article.create({container: App.__container__});

Ember.run(article, article.load, 1, {comments: Ember.A([{id: 'a'}, {id: 'b'}])});

expectAssertion(function() {
article.get('comments');
},
/Models created from store must define relationships with strings not objects. Using convention 'post' not 'App.Post'/);

Ember.run(App, 'destroy');
});