灰烬js @每个深层次,但我有两个深层次的关系 [英] Ember js @each one level deep but I have a two deep level relationship

查看:93
本文介绍了灰烬js @每个深层次,但我有两个深层次的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须访问控制器上两层深的属性,但是 [] 只能通过emberjs指南访问一层深的属性。

I have to access a property that is two level's deep on my controller, but the [] only can access one level deep via the emberjs guide.

model(params) {
    params.paramMapping = {
        page: "page",
        perPage: "per_page",
        total_pages: "pages"
    };

    return this.findPaged('contractf', params);
},

setupController(controller, model) {
    model.forEach(function(item) {
        item.set('sale_price', item.get('dealers_sched_id.sale_price'));
    });

    controller.set('content', model);
},

上面,我有我的模型,该模型基本上是在分页中获取所有记录合同模型中的格式。然后,我设置控制器并遍历所有这些模型,并绑定与其关系中的 sale_price 属性,以正确的模型关系获取sale_price。

Above, I have my model which is basically fetching all records in a pagination format in the contractf model. Then I set up my controller and loop through all those models and bind a sale_price property that goes into it's relationship to get the sale_price in the correct model relation.

现在在我的模板中,我有这个:

Now in my template, I have this:

new_suggested_price: Ember.computed('selectedItems', 'selectedItems.[].sale_price', function() {
    var ret = 0;

    this.get('selectedItems').filterBy('car_used', 'N').forEach(function(contract){
        var salePrice = contract.get('sale_price');

        if (salePrice) {
            ret += (salePrice*100);
        }
    });

    return ret/100; // We *100/100, so we avoid a floating point calc error.
}),

基本上只是给了我一个易于格式化的数字。如您所见,它取决于selectedItems(基本上是模型,但按属性过滤)。因此,我必须进入每个模型项,然后找到我设置的 sale_price 属性,如果更改,此计算属性将更新。阅读Ember的指南,我无法执行 selectedItems。[]。dealers_sched_id.sale_price ,因为它只能深入一个层次。

Basically just gives me a number that is easily format-able. As you can see it depends on the selectedItems (which is basically the model, but filters by a property). So I have to go into each model item and find that sale_price I property I set and if it changes, this computed property will update. Reading Ember's guide, I couldn't do selectedItems.[].dealers_sched_id.sale_price because it only goes one level deep.

我认为在setupController上设置一个属性可以解决该问题,但是似乎没有,因为我仍然以 NaN 作为 sale_price 值。

I thought setting a property on setupController would fix that issue, but it doesn't seem to because I'm still getting NaN as the sale_price value.

现在,如果我将setTimeout函数设置为500毫秒,它会很好地填充。如何在页面加载时定义它

Now if I set a setTimeout function for 500 ms, it populates fine.. How do I get it defined on page load?

谢谢您的帮助。

推荐答案

为什么这样做存在问题



Ember API确实仅允许您使用一个级别的 @each / [] 中的计算属性依赖项。

Why does this problem exist

Ember API indeed allows you to use only one level of @each/[] in your computed property dependencies.

该限制可能是人为的,因为使用了两个或多个 @each 级别对内部观察者维护有巨大的性能影响。

This limitation is likely artificial as using two or more @each levels is a huge performance impact on internal observer maintenance.

因此,您必须避免使用多个 @ CP依赖链中的每个 / [] s。

Thus, you have to avoid more than one @each/[] in your CP dependency chains.

但是有时候您的业务要求您必须达到更高的级别。

But sometimes your business dictates you to have to or more levels.

不要害怕!这可以通过一系列计算属性来实现,其中每个属性只有一个 @每个级别。

Fear not! This can be achieved with a chain of computed properties, where each property has only one @each level.

说,您有Foo,Bar和Baz模型,并希望依赖

Say, you have Foo, Bar and Baz models and want to depend on

foos.@each.bars.@each.bazes.@each.name

这里是您需要创建的一系列计算属性:

Here's a chain of computed properties you need to create:


  • barsArrays:计算值('foos。@ each.bars')-映射 foos 通过 bars 。您将拥有一组由条形组成的数组。

  • bars:compute('barsArrays。[]')-将其展平

  • bazesArrays:compute('bars。@ each.bazes')-地图 bars 通过 bazes

  • bazes:计算('bazesArrays 。[]')-展平 bazesArrays

  • bazesNames:计算('bazes。@ each.name')-通过 name 映射 bazes

  • barsArrays: computed('foos.@each.bars') -- map foos by bars. You'll have an array of arrays of bars.
  • bars: computed('barsArrays.[]') -- flatten it to receive an array of bars.
  • bazesArrays: computed('bars.@each.bazes') -- map bars by bazes.
  • bazes: computed('bazesArrays.[]') -- flatten bazesArrays.
  • bazesNames: computed('bazes.@each.name') -- map bazes by name.

请注意,您可以这样做通过依赖 bar.bazes 是一个关系数组,该关系数组永远不会被其他数组替换(只有其内容会更改,但是数组对象保持不变)。

Note that you can make this chain shorter (but not necessarily more performant) by relying on the fact that bar.bazes is a relationship array that is never replaced with a different array (only its content changes, but the array object stays the same).


  • bazesArrays:计算结果('foos。@ each.bars')-用 bars 映射 foos ,展平,然后按映射惊动。您将拥有一系列的baze。

  • bazes:compute('bazesArrays。[]')-展平 bazesArrays

  • bazesNames:已计算('bazes。@ each.name')-按名称映射抢劫

  • bazesArrays: computed('foos.@each.bars')-- map foos by bars, flatten, then map by bazes. You'll have an array of arrays of bazes.
  • bazes: computed('bazesArrays.[]') -- flatten bazesArrays.
  • bazesNames: computed('bazes.@each.name') -- map bazes by name.

这是一个可行的演示: http:// emberjs .jsbin.com / velayu / 4 / edit?html,js,output

Here's a working demo: http://emberjs.jsbin.com/velayu/4/edit?html,js,output

这篇关于灰烬js @每个深层次,但我有两个深层次的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆