Ember Direct URL findQuery返回整个商店而不是单个记录

时间:2015-03-03 16:06:36

标签: ember.js ember-data

这应该很简单,但我正在努力。找到以下帖子,让我的查询通过slug工作。 EmberJS Direct Slug URL Access Not Working

从链接导航时工作 - 但是根据上面的帖子,返回的模型在直接访问时应该是单个记录数组。然后我应该能够返回数组的第一个对象。然而,我的整个商店都被退回了。所有子类别。 slug在每条记录上都是唯一的,因此它应该只返回一个子类别。所有数据和关系都根据Ember检查器加载(数据选项卡显示所有数据和关系)

这是以下代码的JSBin。 http://emberjs.jsbin.com/qajawopovo/3/edit?html,js,output

  

错误:断言失败:ArrayProxy需要一个Array或Ember.ArrayProxy,但是你传递了对象

这是我的路线代码:

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"},
];

提前感谢您的任何帮助。如果我需要提供更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

我的调试版Ember在控制台上给我这个:在你的小提琴上

未捕获错误:断言失败:您在ID为2的“子类别”中查找“记录”关系,但未加载某些关联记录。要么确保它们都与父记录一起加载,要么指定关系是异步的(DS.hasMany({ async: true })