ember.js – Ember Docs – Readonly嵌套数据

我一直在阅读v2.4的Ember文档,我遇到了一个我不理解的部分:

Some models may have properties that are deeply nested objects of readonly data. The naïve solution would be to define models for each nested object and use hasMany and belongsTo to recreate the nested relationship. However,since readonly data will never need to be updated and saved this often results in the creation of a great deal of code for very little benefit. An alternate approach is to define these relationships using an attribute with no transform (DS.attr()). This makes it easy to access readonly values in computed properties and templates without the overhead of defining extraneous models.

在我的用例中,我只会显示数据,它永远不会保存回服务器.我找到的嵌套数据的每个例子都显示了为每个嵌套级别设置单独的模型,然后建立关系,称为“天真的解决方案”.那么正确的方法是什么呢?任何人都可以请我为此扩展一下吗?提前致谢!

解决方法

看看评论,答案是:

定义根对象并将该对象的字段定义为DS.attr().没有类型信息.

您仍然可以使用点表示法访问嵌套数据,但您不需要再指定任何结构.

请查看以下链接,以便更全面地了解此主题.

https://thejsguy.com/2016/01/29/working-with-nested-data-in-ember-data-models.html

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...