如何在Ember中显示HasMany关系 [英] How to display HasMany relationship in Ember

查看:92
本文介绍了如何在Ember中显示HasMany关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要什么才能使这个简单的 jsfiddle 显示产品列表,例如

What is required to make this simple jsfiddle display a list of categories with products e.g.


  • 蔬菜

    • 胡萝卜

    • 芦笋

    查看代码:

    <script type="text/x-handlebars" data-template-name="categories">
        {{#each}}
            <div>Category: {{name}}</div>
                {{#each product in this.products}}
                    <div>Product: {{product.name}}</div>            
                {{/each}}
        {{/each}}
    </script>
    

    型号代码:

    App.Category = DS.Model.extend({
        name: DS.attr('string'),
        products: DS.hasMany('product')
    });
    
    App.Product = DS.Model.extend({
        name: DS.attr('string')
    });
    

    路线:

    App.CategoriesRoute = Ember.Route.extend({
        model: function () {
            return this.store.find('category');
        }
    });
    


    推荐答案

    您需要使关系异步并修复json (删除ID)

    You need to make the relationship async and fix the json (remove the id)

    异步,因为记录不包括在类别

    async because the records aren't included with the Category

    没有附加的ID,因为这不是Ember Data期望的格式 https://github.com/emberjs/data/ blob / master / TRANSITION.md

    no appended id because that's not the format Ember Data expects https://github.com/emberjs/data/blob/master/TRANSITION.md

    App.Category = DS.Model.extend({
        name: DS.attr('string'),
        products: DS.hasMany('product', {async: true})
    });
    
    App.Category.FIXTURES = [
        {    id: 1,    name: 'Shirts',   products: [1]},
        {    id: 2,    name: 'Pants',    products: [1,2]},
        {    id: 3,    name: 'Socks',    products: [3]},
        {    id: 4,    name: 'Shoes',    products: [3,4]}
    ];
    

    http://jsfiddle.net/2Mguy/

    这篇关于如何在Ember中显示HasMany关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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