Prevent error when trying to get relationship and no links have been loaded

This commit is contained in:
Toby Zerner 2015-05-30 13:58:21 +09:30
parent 731b00571c
commit a3b029accc

View File

@ -80,23 +80,29 @@ export default class Model {
static prop(name, transform) { static prop(name, transform) {
return function() { return function() {
var data = this.data()[name]; var data = this.data()[name];
return transform ? transform(data) : data return transform ? transform(data) : data;
} }
} }
static one(name) { static one(name) {
return function() { return function() {
var link = this.data().links[name]; var data = this.data();
return link && app.store.getById(link.linkage.type, link.linkage.id) if (data.links) {
var link = data.links[name];
return link && app.store.getById(link.linkage.type, link.linkage.id);
}
} }
} }
static many(name) { static many(name) {
return function() { return function() {
var link = this.data().links[name]; var data = this.data();
return link && link.linkage.map(function(link) { if (data.links) {
return app.store.getById(link.type, link.id) var link = this.data().links[name];
}) return link && link.linkage.map(function(link) {
return app.store.getById(link.type, link.id)
});
}
} }
} }