【问题标题】:Using embedded objects with FIXTURES使用带有 FIXTURES 的嵌入对象
【发布时间】:2013-08-22 11:09:18
【问题描述】:

使用REST Adapter,可以通过以下方式嵌入对象:

App.Adapter.map('App.User', {
    properties : {embedded: 'always'},
});

如何使用FIXTURE 声明嵌入对象?我已经尝试在模型中指定这个:

App.User = DS.Model.extend({
    properties : DS.belongsTo('App.UserProperties', { embedded : true })
});

但这不起作用。是否可以使用FIXTURES 嵌入对象?我的FIXTURE 看起来像:

App.User.FIXTURES = [
    { 
        id: 'id1',
        type: 'user', 
        properties : { 
            name: 'Max Smith',
            email: 'max.smith@email.com' }
    },
];

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    FixtureAdapter 不支持嵌入记录,唯一的方法是为您的 UserProperties 模型定义 FIXTURES

    App.User.FIXTURES = [
      { 
        id: 'id1',
        type: 'user', 
        properties : 1
    ];
    
    App.UserProperties.FIXTURES = [
      { 
        id: '1',
        name: 'Max Smith',
        email: 'max.smith@email.com'
      }
    ];
    

    这里还有来自@tomdale(ember-data 的创建者之一)在FixtureAdapter 上的简短声明

    我不相信夹具适配器支持嵌入式记录 是否有充分的理由需要这样做? 它也不支持 underscored_property_names 夹具适配器的想法不是模仿来自服务器的 JSON 有效负载 它以 Ember Data 期望的格式提供存根数据 所以没有嵌入关系,属性名称是驼峰式的,等等。

    希望对你有帮助。

    【讨论】:

    • 很公平。但是,从我的角度来看,这违背了 FIXTURES 的一个重要目标,即拥有与后端提供的 exact 相同的 json,并且能够像后端一样测试应用程序,包含所有配置的映射和其他适配器配置。使用所述方法,我必须手动编辑 json,这意味着我可以()出错。所以现在我的“测试数据”必须被测试。不好。
    猜你喜欢
    • 2022-01-19
    • 2018-05-10
    • 2016-02-07
    • 1970-01-01
    • 2017-12-02
    • 2013-12-30
    • 2018-07-21
    • 2012-02-03
    • 2011-11-15
    相关资源
    最近更新 更多