你能重构一下这段代码吗?

时间:2015-01-26 23:14:52

标签: coffeescript parse-platform refactoring

如何重构这个返回json对象的coffeescript方法?

getQuery: ->
  p =
    user:
      __type: "Pointer"
      className: '_User'
      objectId: @model.id
    private: false

  q =
    user:
      __type: "Pointer"
      className: '_User'
      objectId: @model.id

  spec =
    projects:
      collection: "Projects"
      params:
        where: JSON.stringify(p)
      needsFetch: true

    payments:
      collection: "Payments"
      params:
        where: JSON.stringify(q)
      needsFetch: true

var' p'和' q'几乎相同,但是' p'有{private:true}

提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果使用下划线,则可以扩展p本质上是字典:

q = _.extend p, {private:false}