Ember 直接 URL findQuery returns 整个商店而不是单个记录
Ember Direct URL findQuery returns entire store rather than single record
这应该很简单,但我很费力。找到以下 post 来让我的查询正常工作。 EmberJS Direct Slug URL Access Not Working
在从 link 导航到时有效,但是根据上述 post,模型 returned 在直接访问时应该是单个记录数组。然后我应该能够 return 数组的第一个对象。然而,我的整个商店都得到了 returned。所有子类别。 slug 在每条记录上都是唯一的,因此它应该只有 return 一个子类别。所有数据和关系都根据 Ember 检查器加载(数据选项卡显示所有内容)
这是以下代码的 JSBin。 http://emberjs.jsbin.com/qajawopovo/3/edit?html,js,output
Error: Assertion Failed: ArrayProxy expects an Array or Ember.ArrayProxy, but you passed object
这是我的路线代码:
App.Router.map(function () {
this.resource("section");
this.resource('subcategory', { path: "/section/:slug" }, function () {
this.resource("record", { path: ":id" });
});
});
App.SectionRoute = Ember.Route.extend({
model: function (params) {
Ember.Logger.warn("Inside section route");
return this.store.find('subcategory'); // gets all subcategories perfectly
},
setupController: function (controller, model) {
this.controller.set('model', model);
this.controllerFor('sectionnav').set('model', model); //set section nav to list of subcats
}
});
App.SubcategoryRoute = Ember.Route.extend({
model: function (params) {
Ember.Logger.warn("Passed Param: " + params.slug);
return this.store.find('subcategory', { slug: params.slug });
},
serialize: function (model) {
//ANOTHER POSSIBLE ISSUE? THIS IS BEING HIT MULTIPLE TIMES....up to 20 when on PREVIOUS route And 13 times when going to this route directly in url address. THere are 5 subcategories total. With many records. Why??
Ember.Logger.warn("Serializing in subcategoryroute");
return { slug: model.get('slug') };
},
setupController: function (controller, model) {
//If the model comes from a link-to helper it will be an object, if it comes from the route it will be an array of one element
//model is the entire store rather than the one record when going to the route directly. WHY??
if (Ember.isArray(model)) {
this.controller.set('model', model.get('firstObject'));
} else {
this.controller.set('model', model); //this works fine from link-to
}
themodel = this.store.find('subcategory');
this.controllerFor('sectionnav').set('model', themodel);
}
});
以下是模型(包括嵌入式记录,以防这些是原因):
App.Subcategory = DS.Model.extend({
name: DS.attr('string'),
slug: DS.attr('string'),
records: DS.hasMany('record', { embedded: 'always', async: true })
});
Ember.Inflector.inflector.irregular("subcategory", "subcategories");
App.Record= DS.Model.extend({
name: DS.attr('string'),
comments: DS.hasMany('comment', { embedded: 'always', async: true }),
events: DS.hasMany('event', { embedded: 'always', async: true })
});
App.Comment = DS.Model.extend({
title: DS.attr('string')
});
App.Event = DS.Model.extend({
name: DS.attr('string'),
eventdates: DS.hasMany('eventdate', { embedded: 'always' })
});
App.Eventdate = DS.Model.extend({
eventDate: DS.attr('string'),
startTime: DS.attr('string')
});
数据(我正在使用 RESTAdapter,但这是 FIXTURES 中的子类别数据):
App.Subcategory.FIXTURES = [
{id: 1, name: "First Category", slug: "firstcategory", records:[1,3]},
{id: 2, name: "Second Category", slug: "secondcategory", records:[2] },
{id: 3, name: "Third Category", slug: "thirdcategory", records:[5,6] },
{id: 4, name: "Fourth Category", slug: "fourthcategory", records:[4] },
{id: 5, name: "Fifth Category", slug: "fifthcategory", records:[7,8] }
];
App.Record.FIXTURES = [
{id:1, name: "Record One", comments: [1], events: [1,2]},
{id:2, name: "Record Two", comments: [2,3], events: []},
{id:3, name: "Record Three", comments: [4], events: [3]},
{id:4, name: "Record Four", comments: [], events: []},
{id:5, name: "Record Five", comments: [6], events: [4]},
{id:6, name: "Record Six", comments: [], events: []},
{id:7, name: "Record Seven", comments: [7], events: []},
{id:8, name: "Record Eight", comments: [], events: []}
];
App.Comment.FIXTURES = [
{id:1, name: "Comment One"},
{id:2, name: "Comment Two"},
{id:3, name: "Comment Three"},
{id:4, name: "Comment Four"},
{id:5, name: "Comment Five"},
{id:6, name: "Comment Six"},
{id:7, name: "Comment Seven"},
{id:8, name: "Record Eight"}
];
App.Event.FIXTURES = [
{id:1, name: "Event One", eventDates: [1]},
{id:2, name: "Event Two",eventDates: [2,3]},
{id:3, name: "Event Three",eventDates: [4]},
{id:4, name: "Event Four",eventDates: [5]}
];
App.Eventdate.FIXTURES = [
{id:1, eventDate: "4/5/2015", startTime: "07:00"},
{id:2, eventDate: "6/9/2015", startTime: "08:00"},
{id:3, eventDate: "5/15/2015", startTime: "15:00"},
{id:4, eventDate: "3/25/2015", startTime: "14:00"},
{id:5, eventDate: "4/5/2015", startTime: "11:00"},
];
在此先感谢您的帮助。如果我需要包含更多信息,请告诉我。
我的调试版本 Ember 在控制台上给我这个:在你的 fiddle
未捕获错误:断言失败:您在 ID 为 2 的 'subcategory' 上查找了 'records' 关系,但未加载一些相关记录。要么确保它们都与父记录一起加载,要么指定关系是异步的 (DS.hasMany({ async: true })
)
这应该很简单,但我很费力。找到以下 post 来让我的查询正常工作。 EmberJS Direct Slug URL Access Not Working
在从 link 导航到时有效,但是根据上述 post,模型 returned 在直接访问时应该是单个记录数组。然后我应该能够 return 数组的第一个对象。然而,我的整个商店都得到了 returned。所有子类别。 slug 在每条记录上都是唯一的,因此它应该只有 return 一个子类别。所有数据和关系都根据 Ember 检查器加载(数据选项卡显示所有内容)
这是以下代码的 JSBin。 http://emberjs.jsbin.com/qajawopovo/3/edit?html,js,output
Error: Assertion Failed: ArrayProxy expects an Array or Ember.ArrayProxy, but you passed object
这是我的路线代码:
App.Router.map(function () {
this.resource("section");
this.resource('subcategory', { path: "/section/:slug" }, function () {
this.resource("record", { path: ":id" });
});
});
App.SectionRoute = Ember.Route.extend({
model: function (params) {
Ember.Logger.warn("Inside section route");
return this.store.find('subcategory'); // gets all subcategories perfectly
},
setupController: function (controller, model) {
this.controller.set('model', model);
this.controllerFor('sectionnav').set('model', model); //set section nav to list of subcats
}
});
App.SubcategoryRoute = Ember.Route.extend({
model: function (params) {
Ember.Logger.warn("Passed Param: " + params.slug);
return this.store.find('subcategory', { slug: params.slug });
},
serialize: function (model) {
//ANOTHER POSSIBLE ISSUE? THIS IS BEING HIT MULTIPLE TIMES....up to 20 when on PREVIOUS route And 13 times when going to this route directly in url address. THere are 5 subcategories total. With many records. Why??
Ember.Logger.warn("Serializing in subcategoryroute");
return { slug: model.get('slug') };
},
setupController: function (controller, model) {
//If the model comes from a link-to helper it will be an object, if it comes from the route it will be an array of one element
//model is the entire store rather than the one record when going to the route directly. WHY??
if (Ember.isArray(model)) {
this.controller.set('model', model.get('firstObject'));
} else {
this.controller.set('model', model); //this works fine from link-to
}
themodel = this.store.find('subcategory');
this.controllerFor('sectionnav').set('model', themodel);
}
});
以下是模型(包括嵌入式记录,以防这些是原因):
App.Subcategory = DS.Model.extend({
name: DS.attr('string'),
slug: DS.attr('string'),
records: DS.hasMany('record', { embedded: 'always', async: true })
});
Ember.Inflector.inflector.irregular("subcategory", "subcategories");
App.Record= DS.Model.extend({
name: DS.attr('string'),
comments: DS.hasMany('comment', { embedded: 'always', async: true }),
events: DS.hasMany('event', { embedded: 'always', async: true })
});
App.Comment = DS.Model.extend({
title: DS.attr('string')
});
App.Event = DS.Model.extend({
name: DS.attr('string'),
eventdates: DS.hasMany('eventdate', { embedded: 'always' })
});
App.Eventdate = DS.Model.extend({
eventDate: DS.attr('string'),
startTime: DS.attr('string')
});
数据(我正在使用 RESTAdapter,但这是 FIXTURES 中的子类别数据):
App.Subcategory.FIXTURES = [
{id: 1, name: "First Category", slug: "firstcategory", records:[1,3]},
{id: 2, name: "Second Category", slug: "secondcategory", records:[2] },
{id: 3, name: "Third Category", slug: "thirdcategory", records:[5,6] },
{id: 4, name: "Fourth Category", slug: "fourthcategory", records:[4] },
{id: 5, name: "Fifth Category", slug: "fifthcategory", records:[7,8] }
];
App.Record.FIXTURES = [
{id:1, name: "Record One", comments: [1], events: [1,2]},
{id:2, name: "Record Two", comments: [2,3], events: []},
{id:3, name: "Record Three", comments: [4], events: [3]},
{id:4, name: "Record Four", comments: [], events: []},
{id:5, name: "Record Five", comments: [6], events: [4]},
{id:6, name: "Record Six", comments: [], events: []},
{id:7, name: "Record Seven", comments: [7], events: []},
{id:8, name: "Record Eight", comments: [], events: []}
];
App.Comment.FIXTURES = [
{id:1, name: "Comment One"},
{id:2, name: "Comment Two"},
{id:3, name: "Comment Three"},
{id:4, name: "Comment Four"},
{id:5, name: "Comment Five"},
{id:6, name: "Comment Six"},
{id:7, name: "Comment Seven"},
{id:8, name: "Record Eight"}
];
App.Event.FIXTURES = [
{id:1, name: "Event One", eventDates: [1]},
{id:2, name: "Event Two",eventDates: [2,3]},
{id:3, name: "Event Three",eventDates: [4]},
{id:4, name: "Event Four",eventDates: [5]}
];
App.Eventdate.FIXTURES = [
{id:1, eventDate: "4/5/2015", startTime: "07:00"},
{id:2, eventDate: "6/9/2015", startTime: "08:00"},
{id:3, eventDate: "5/15/2015", startTime: "15:00"},
{id:4, eventDate: "3/25/2015", startTime: "14:00"},
{id:5, eventDate: "4/5/2015", startTime: "11:00"},
];
在此先感谢您的帮助。如果我需要包含更多信息,请告诉我。
我的调试版本 Ember 在控制台上给我这个:在你的 fiddle
未捕获错误:断言失败:您在 ID 为 2 的 'subcategory' 上查找了 'records' 关系,但未加载一些相关记录。要么确保它们都与父记录一起加载,要么指定关系是异步的 (DS.hasMany({ async: true })
)