为什么我不能在嵌套的每个循环中显示第二个关联?

时间:2021-06-15 23:08:20

I have 2 models deliverHour and day. In an .hbs file, I have two nested each loops.

我有2个模特送货时间和日。在.hbs文件中,我有两个嵌套的每个循环。

I am trying to display the delivery hours for each day. Apparently, once it reaches the second each loop, it doesnt render anything. Not even this line does not render!

我想显示每天的交货时间。显然,一旦它到达第二个循环,它就不会呈现任何东西。甚至这条线也不会渲染!

Looking in Ember Inspector. I do not see any failed promises. The network tab doesn't even show Ember trying to attempt calling the delivery hour end point. The Data tab shows delivery-hour (0), which means no delivery hours in the store, which is strange.

看着灰烬检查员。我没有看到任何失败的承诺。网络选项卡甚至不显示Ember试图尝试呼叫传递小时结束点。 “数据”选项卡显示交货时间(0),这意味着商店中没有交货时间,这很奇怪。

Since the relationship for the day model has DS.hasMany('deliveryHour', { async: true }) I would have expected Ember to do an asynchronous call to the delivery hour api resource. No?

由于日模型的关系有DS.hasMany('deliveryHour',{async:true}),我原以为Ember会对交付时api资源进行异步调用。没有?

What am I missing here?

我在这里想念的是什么?

// Route
import Ember from 'ember';

export default Ember.Route.extend({
  model: function(params) {
    var addressId = this.checkoutCart.get('addressId');

    return Ember.RSVP.hash({
      address:       this.store.find('address', addressId),
      delivery:      this.store.createRecord('delivery'),
      days:          this.store.find('day')
    });
  },

  // You can use model.delivery to get delivery, etc
  // Since the model is a plain object you can just use setProperties
  // http://*.com/questions/16463958/how-to-use-multiple-models-with-a-single-route-in-emberjs-ember-data
  setupController: function(controller, model) {
    controller.setProperties(model);
  },
});

// app/models/delivery-hour.js
import DS from 'ember-data';

export default DS.Model.extend({
  day:    DS.belongsTo('day', { async: true }),
  from:   DS.belongsTo('hour', { async: true }),
  to:     DS.belongsTo('hour', { async: true }),
  status: DS.attr('string')
});

// app/models/day.js
import DS from 'ember-data';

export default DS.Model.extend({
  deliveryHours: DS.hasMany('deliveryHour', { async: true }),
  name:          DS.attr('string'),
  dayOfTheWeek:  DS.attr('number')
});

// hbs
{{#each day in days}}
  <li>
    {{day.name}} // Returns something like 'Monday'
    {{day.deliveryHours}} // Returns something like <(subclass of DS.PromiseArray):ember770>
    {{day.deliveryHours.length}} // Returns 0

    {{#each deliveryHour in day.deliveryHours}}
      this line does not render!
      {{deliveryHour.hour}} <----- ideally, this is what i want to see
    {{/each}}
  </li>
{{/each}}

{{day.deliveryHours.length}} returns 0. I wanted to verify this. I am using Rails as my API. In Rails console, when I do Day.first.delivery_hours.count, it returns 4. Strange that Ember returns 0. The Network tab, in Ember Inspector, does not show Ember trying to call the delivery hours API endpoint.

{{day.deliveryHours.length}}返回0.我想验证这一点。我使用Rails作为我的API。在Rails控制台中,当我执行Day.first.delivery_hours.count时,它返回4.确定Ember返回0.Ember Inspector中的Network选项卡不显示Ember尝试调用交付时间API端点。

On the top right corner of the screenshot, I click on the > $E. Then I go to the console tab of Ember inspector and paste: $E.get('deliveryHours'). I get Object { content: Getter, toString: makeToString/<(), isFulfilled: true, 4 more… }.

在屏幕截图的右上角,我点击> $ E。然后我转到Ember检查器的控制台选项卡并粘贴:$ E.get('deliveryHours')。我得到Object {content:Getter,toString:makeToString / <(),isFulfilled:true,4 more ...}。

In the console of Ember Inspector, I also tried:

在Ember Inspector的控制台中,我也尝试过:

>> $E.get('deliveryHours').then(function(hours) { console.log(hours.get('length'));});
Object { _id: 156, _label: undefined, _state: undefined, _result: undefined, _subscribers: Array[0] }

为什么我不能在嵌套的每个循环中显示第二个关联?

Update: My API response for days returns as:

更新:我的API响应天数返回为:

{
    "days": 
[
{

    "id": 1,
    "name": "Monday",
    "day_of_the_week": 1

},
{

    "id": 2,
    "name": "Tuesday",
    "day_of_the_week": 2

},
{

    "id": 3,
    "name": "Wednesday",
    "day_of_the_week": 3

},
{

    "id": 4,
    "name": "Thursday",
    "day_of_the_week": 4

},
{

    "id": 5,
    "name": "Friday",
    "day_of_the_week": 5

},
{

    "id": 6,
    "name": "Saturday",
    "day_of_the_week": 6
},
{
    "id": 7,
    "name": "Sunday",
    "day_of_the_week": 7
}
]
}

http://emberjs.com/guides/models/the-rest-adapter/#toc_relationships indicates that I should probably load the delivery_hours as an array of ID's. I've tried this too, same results..

http://emberjs.com/guides/models/the-rest-adapter/#toc_relationships表明我应该将delivery_hours加载为ID的数组。我也试过这个,结果相同..

1 个解决方案

#1


1  

That's because deliveryHours is an async property in your model, which is why {{day.deliverHours}} returns an unresolved promise, instead of the record array you were expecting.

这是因为deliveryHours是模型中的异步属性,这就是{{day.deliverHours}}返回未解析的promise而不是您期望的记录数组的原因。

You'll have to explicitly resolve it in an afterModel hook, like so:

你必须在afterModel钩子中显式解决它,如下所示:

// Route
import Ember from 'ember';

var RSVP = Ember.RSVP;

export default Ember.Route.extend({
  // ...

  afterModel: function(model) {
    return RSVP.all(
      model.days.map(function(day) {
        return day.get('deliveryHours');
      })
    );
  }

});

#1


1  

That's because deliveryHours is an async property in your model, which is why {{day.deliverHours}} returns an unresolved promise, instead of the record array you were expecting.

这是因为deliveryHours是模型中的异步属性,这就是{{day.deliverHours}}返回未解析的promise而不是您期望的记录数组的原因。

You'll have to explicitly resolve it in an afterModel hook, like so:

你必须在afterModel钩子中显式解决它,如下所示:

// Route
import Ember from 'ember';

var RSVP = Ember.RSVP;

export default Ember.Route.extend({
  // ...

  afterModel: function(model) {
    return RSVP.all(
      model.days.map(function(day) {
        return day.get('deliveryHours');
      })
    );
  }

});