简体   繁体   English

是否在ember数据中可为null属于所属关系?

[英]Nullable belongsTo relationship in ember-data?

I'm using ember-data v1.0.0-beta.3, active model serializers and the DS.ActiveModelAdapter. 我正在使用ember-data v1.0.0-beta.3,活动模型序列化程序和DS.ActiveModelAdapter。 I have a model that looks something like this: 我有一个看起来像这样的模型:

App.Listing = DS.Model.extend
 title: DS.attr()
 pickupAddress: DS.belongsTo("address")

App.Address = DS.Model.extend
  listing: DS.belongsTo("listing")
  address: DS.attr()

I want the pickupAddress field to be optional, and my JSON for /listings/{id} looks like this: 我希望PickupAddress字段是可选的,并且我的/ listings / {id}的JSON如下所示:

{
  "pickup_addresses":[],
  "listing":{"id":2,"title":"Foobar", "pickup_address_id":null}
}

However, ember-data doesn't like this, it gives me this error: 但是,ember-data不喜欢这样,它给了我这个错误:

Assertion failed: No model was found for 'pickupAddress' 断言失败:未找到“ pickupAddress”的模型

Does it support this scenario? 它支持这种情况吗? Is there some option I can pass to the belongsTo? 有什么我可以传递给belongsTo的选项吗? Or should I create some custom adapter? 还是应该创建一些自定义适配器?

Thanks 谢谢

The problem is with your payload, the sideloaded data need to match a class name, in your sample you have pickup_addresses , the correct is addresses : 问题是你的有效载荷,传输的音乐数据需要匹配一个类名,您的样品必须在pickup_addresses ,正确的是addresses

{
    "addresses": [],
    "listing": {
        "id": 2,
        "title": "Foobar",
        "pickup_address_id": null
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM