Skip to content
Merged
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
3 changes: 2 additions & 1 deletion addon/adapters/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ export default Ember.Object.extend(Ember.Evented, {
resource = resource.resource;
type = resource.type;
}
let service = this.container.lookup('service:' + pluralize(type));
// use resource's service if in container, otherwise use this service to fetch
let service = this.container.lookup('service:' + pluralize(type)) || this;
url = this.fetchUrl(url);
return service.fetch(url, { method: 'GET' });
},
Expand Down
35 changes: 18 additions & 17 deletions addon/models/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,26 +280,27 @@ Resource.reopenClass({
}
let type = instance.get('type');
let msg = (type) ? Ember.String.capitalize(singularize(type)) : 'Resource';
let factory = 'model:' + type;
if (!type) {
Ember.Logger.warn(msg + '#create called, instead you should first use ' + msg + '.extend({type:"entity"})');
}
let factory = 'model:' + type;
if (instance.container) {
factory = instance.container.lookupFactory(factory);
let proto = factory.proto();
factory.eachComputedProperty(function(prop) {
if (proto[prop] && proto[prop]._meta && typeof proto[prop]._meta === 'object') {
if (proto[prop]._meta.kind === 'hasOne') {
setupRelationship.call(instance, prop);
} else if (proto[prop]._meta.kind === 'hasMany') {
setupRelationship.call(instance, prop, Ember.A([]));
}
}
});
} else {
msg += '#create should only be called from a container lookup (relationships not setup) ';
msg += 'use this.container.lookupFactory("' + factory + '").create() instead.';
Ember.Logger.warn(msg);
if (instance.container) {
factory = instance.container.lookupFactory(factory);
let proto = factory.proto();
factory.eachComputedProperty(function(prop) {
if (proto[prop] && proto[prop]._meta && typeof proto[prop]._meta === 'object') {
if (proto[prop]._meta.kind === 'hasOne') {
setupRelationship.call(instance, prop);
} else if (proto[prop]._meta.kind === 'hasMany') {
setupRelationship.call(instance, prop, Ember.A([]));
}
}
});
} else {
msg += '#create should only be called from a container lookup (relationships not setup) ';
msg += 'use this.container.lookupFactory("' + factory + '").create() instead.';
Ember.Logger.warn(msg);
}
}
return instance;
}
Expand Down
206 changes: 206 additions & 0 deletions fixtures/api/pictures.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
{
"data": [
{
"id": "1",
"type": "pictures",
"links": {
"self": "http://api.pixelhandler.com/api/v1/pictures/1"
},
"attributes": {
"name": "box of chocolates"
},
"relationships": {
"imageable": {
"links": {
"self": "http://api.pixelhandler.com/api/v1/pictures/1/relationships/imageable",
"related": "http://api.pixelhandler.com/api/v1/pictures/1/imageable"
},
"data": {
"type": "products",
"id": "3"
}
}
}
},
{
"id": "2",
"type": "pictures",
"links": {
"self": "http://api.pixelhandler.com/api/v1/pictures/2"
},
"attributes": {
"name": "10 foot candy cane"
},
"relationships": {
"imageable": {
"links": {
"self": "http://api.pixelhandler.com/api/v1/pictures/2/relationships/imageable",
"related": "http://api.pixelhandler.com/api/v1/pictures/2/imageable"
},
"data": {
"type": "products",
"id": "2"
}
}
}
},
{
"id": "3",
"type": "pictures",
"links": {
"self": "http://api.pixelhandler.com/api/v1/pictures/3"
},
"attributes": {
"name": "Hot apple fritter"
},
"relationships": {
"imageable": {
"links": {
"self": "http://api.pixelhandler.com/api/v1/pictures/3/relationships/imageable",
"related": "http://api.pixelhandler.com/api/v1/pictures/3/imageable"
},
"data": {
"type": "products",
"id": "1"
}
}
}
},
{
"id": "4",
"type": "pictures",
"links": {
"self": "http://api.pixelhandler.com/api/v1/pictures/4"
},
"attributes": {
"name": "Boston Creme"
},
"relationships": {
"imageable": {
"links": {
"self": "http://api.pixelhandler.com/api/v1/pictures/4/relationships/imageable",
"related": "http://api.pixelhandler.com/api/v1/pictures/4/imageable"
},
"data": {
"type": "products",
"id": "1"
}
}
}
},
{
"id": "5",
"type": "pictures",
"links": {
"self": "http://api.pixelhandler.com/api/v1/pictures/5"
},
"attributes": {
"name": "Bill at EmberConf"
},
"relationships": {
"imageable": {
"links": {
"self": "http://api.pixelhandler.com/api/v1/pictures/5/relationships/imageable",
"related": "http://api.pixelhandler.com/api/v1/pictures/5/imageable"
},
"data": {
"type": "employees",
"id": "1"
}
}
}
}
],
"included": [
{
"id": "3",
"type": "products",
"links": {
"self": "http://api.pixelhandler.com/api/v1/products/3"
},
"attributes": {
"name": "Chocolates"
},
"relationships": {
"pictures": {
"links": {
"self": "http://api.pixelhandler.com/api/v1/products/3/relationships/pictures",
"related": "http://api.pixelhandler.com/api/v1/products/3/pictures"
}
}
}
},
{
"id": "2",
"type": "products",
"links": {
"self": "http://api.pixelhandler.com/api/v1/products/2"
},
"attributes": {
"name": "Candy Canes"
},
"relationships": {
"pictures": {
"links": {
"self": "http://api.pixelhandler.com/api/v1/products/2/relationships/pictures",
"related": "http://api.pixelhandler.com/api/v1/products/2/pictures"
}
}
}
},
{
"id": "1",
"type": "products",
"links": {
"self": "http://api.pixelhandler.com/api/v1/products/1"
},
"attributes": {
"name": "Donuts"
},
"relationships": {
"pictures": {
"links": {
"self": "http://api.pixelhandler.com/api/v1/products/1/relationships/pictures",
"related": "http://api.pixelhandler.com/api/v1/products/1/pictures"
}
}
}
},
{
"id": "1",
"type": "employees",
"links": {
"self": "http://api.pixelhandler.com/api/v1/employees/1"
},
"attributes": {
"name": "Bill Heaton"
},
"relationships": {
"pictures": {
"links": {
"self": "http://api.pixelhandler.com/api/v1/employees/1/relationships/pictures",
"related": "http://api.pixelhandler.com/api/v1/employees/1/pictures"
}
}
}
}
],
"meta": {
"page": {
"total": 7,
"sort": [
{
"field": "id",
"direction": "asc"
}
],
"offset": 0,
"limit": 5
}
},
"links": {
"first": "http://api.pixelhandler.com/api/v1/pictures?include=imageable&page%5Blimit%5D=5&page%5Boffset%5D=0",
"next": "http://api.pixelhandler.com/api/v1/pictures?include=imageable&page%5Blimit%5D=5&page%5Boffset%5D=5",
"last": "http://api.pixelhandler.com/api/v1/pictures?include=imageable&page%5Blimit%5D=5&page%5Boffset%5D=2"
}
}
20 changes: 20 additions & 0 deletions fixtures/api/pictures/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"data": {
"id": "1",
"type": "pictures",
"links": {
"self": "http://api.pixelhandler.com/api/v1/pictures/1"
},
"attributes": {
"name": "box of chocolates"
},
"relationships": {
"imageable": {
"links": {
"self": "http://api.pixelhandler.com/api/v1/pictures/1/relationships/imageable",
"related": "http://api.pixelhandler.com/api/v1/pictures/1/imageable"
}
}
}
}
}
20 changes: 20 additions & 0 deletions fixtures/api/pictures/1/imageable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"data": {
"id": "3",
"type": "products",
"links": {
"self": "http://api.pixelhandler.com/api/v1/products/3"
},
"attributes": {
"name": "Chocolates"
},
"relationships": {
"pictures": {
"links": {
"self": "http://api.pixelhandler.com/api/v1/products/3/relationships/pictures",
"related": "http://api.pixelhandler.com/api/v1/products/3/pictures"
}
}
}
}
}
20 changes: 20 additions & 0 deletions fixtures/api/pictures/5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"data": {
"id": "5",
"type": "pictures",
"links": {
"self": "http://api.pixelhandler.com/api/v1/pictures/5"
},
"attributes": {
"name": "Bill at EmberConf"
},
"relationships": {
"imageable": {
"links": {
"self": "http://api.pixelhandler.com/api/v1/pictures/5/relationships/imageable",
"related": "http://api.pixelhandler.com/api/v1/pictures/5/imageable"
}
}
}
}
}
20 changes: 20 additions & 0 deletions fixtures/api/pictures/5/imageable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"data": {
"id": "1",
"type": "employees",
"links": {
"self": "http://api.pixelhandler.com/api/v1/employees/1"
},
"attributes": {
"name": "Bill Heaton"
},
"relationships": {
"pictures": {
"links": {
"self": "http://api.pixelhandler.com/api/v1/employees/1/relationships/pictures",
"related": "http://api.pixelhandler.com/api/v1/employees/1/pictures"
}
}
}
}
}
Loading