【问题标题】:Multiple belongsTo Associations with Batman.jsBatman.js 的多个 belongsTo 关联
【发布时间】:2013-11-19 14:08:04
【问题描述】:

我只是想检查具有 2 个 belongsTo 的模型是否有问题:

@belongsTo 'claim'
@belongsTo 'buyer'

我问是因为我有这个并且保存不会产生它应该产生的 json:

create: =>
    alert @claim
    CT.Buyer.find $("#buyer_id").val(), (err, buyer) =>
        @bid.set 'claim', @claim
        @bid.set 'buyer', buyer
        @bid.save()

    return false

alert @claim 清楚地告诉我@claim 是正确的并且包含我期望的声明。但是调用save()时发送的json看起来像:

{buyer_id:52c86c74-2425-11e1-8b23-0021cc5da1e1, amount:123}

由于某种原因,它没有发送claim_id

【问题讨论】:

    标签: coffeescript batman.js


    【解决方案1】:

    一个模型上可以有多个 belongsTo 关联。您确定您要寻找的买家存在吗?您应该始终在 find 回调中处理 err

    【讨论】:

      【解决方案2】:

      您的型号代码中有encode-ing claim_id 吗?除非您使用 encode,否则 Batman.Model 不会以 JSON 格式发回值。例如:

      class App.Bid extends Batman.Model
        @belongsTo 'claim'
        @belongsTo 'buyer'
        @encode 'claim_id', 'buyer_id'
      

      您也可以使用encodeForegnKey 选项:

      class App.Bid extends Batman.Model
        @belongsTo 'claim', encodeForeignKey: true
        @belongsTo 'buyer', encodeForeignKey: true
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-05
        • 1970-01-01
        • 1970-01-01
        • 2019-02-15
        • 2018-07-13
        • 1970-01-01
        • 2020-06-25
        相关资源
        最近更新 更多