【问题标题】:Failed to implement computed property in emberjs无法在 emberjs 中实现计算属性
【发布时间】:2013-09-06 16:41:29
【问题描述】:

我的夹具数据包含多个数组。在这个多个数组中cart_items 包含一些产品数据。

我正在尝试计算购物车数据中可用产品的总数(基于 cart_items 项目的长度),但我无法计算 cart_items 中存在的项目数量。

在路由器中,我选择了应用程序夹具作为当前路由的模型,如下:

 Astcart.IndexRoute = Ember.Route.extend({
    model: function() {
      return Astcart.Application.find();
    }
  }); 

计算属性代码:

Astcart.IndexController = Ember.ArrayController.extend({
     tot_cart_prd: function() {
          return this.get("model.cart_items").get('length');
      }.property("@each.isLoaded")
});

我的夹具数据是:

Astcart.Application.adapter = Ember.FixtureAdapter.create();

Astcart.Application.FIXTURES = [
    {

        "logo_url": "img/logo.jpg",
        "logged_in": {
            "logged": true,
            "username": "sachin",
            "account_id": "4214"
        },
        "category_list": [
            {
                "id": "1",
                "name": "Mobiles & Accessories"
            },
            {
                "id": "2",
                "name": "Computers & Software"
            },
            {
                "id": "3",
                "name": "Fashion"
            },
            {
                "id": "4",
                "name": "Electronics"
            },
            {
                "id": "5",
                "name": "Watches & Jewelry"
            },
            {
                "id": "6",
                "name": "Health & Beauty"
            },
            {
                "id": "7",
                "name": "Games"
            },
            {
                "id": "8",
                "name": "Books & Entertainment"
            },
            {
                "id": "9",
                "name": "Gaming"
            },
            {
                "id": "10",
                "name": "Shoes & Bags"
            }
        ],
        "cart_items": [
            {
                "id": "1",
                "name": "Samsung Galaxy Tab 2",
                "qty": "1",
                "price": "1245.12",
                "subtotal": "7842.23"
            },
            {
                "id": "2",
                "name": "Samsung Galaxy Tab 2",
                "qty": "1",
                "price": "1245.12",
                "subtotal": "7842.23"
            },
            {
                "id": "3",
                "name": "Samsung Galaxy Tab 2",
                "qty": "1",
                "price": "1245.12",
                "subtotal": "7842.23"
            }
        ]           
    }
];

我已经发布了我的代码here(JSFiddle)

谁能告诉我为什么this.get("model.cart_items")返回null?

【问题讨论】:

    标签: ember.js handlebars.js ember-model


    【解决方案1】:

    因为您的 IndexController 从路由中接收到 Astcart.Application 数组。您需要在每个应用程序中进行迭代并获取每个类别列表的长度。

    您的计算属性需要如下:

    Astcart.IndexController = Ember.ArrayController.extend({
        tot_cart_prd: function() {
            var result = this.get('model').map(function(application) {
                return application.get('category_list.length');
            });
            return result;
        }.property('model.@each.category_list.length')
    });
    

    这是一个更新的小提琴http://jsfiddle.net/marciojunior/PZZym/

    【讨论】:

      【解决方案2】:

      我刚刚看了这个,您的核心问题与ApplicationCart_items 之间的关系设置有关。 this.get("model.cart_items").get('length') 失败的原因是 this.get("model.cart_items") 返回 null。如果你能让你的关系正常运转,你应该走在正确的轨道上。我对 EmberModel 一无所知,所以我帮不上什么忙。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多