如何在Ember中显示HasMany关系

编程入门 行业动态 更新时间:2024-10-19 09:30:51
本文介绍了如何在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期望的格式 github/emberjs/data/ blob / master / TRANSITION.md

    no appended id because that's not the format Ember Data expects github/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]} ];

    jsfiddle/2Mguy/

更多推荐

如何在Ember中显示HasMany关系

本文发布于:2023-10-24 20:15:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524898.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系   如何在   Ember   HasMany

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!