1

我对 Backbone.js 有疑问。我的系统在 ID 的模式上存在不一致。

事实上,系统有两种类型的 ID:

1) id: 45 (整数,默认且工作正常) 2) id: app-45 (有问题)

第一个问题是主干“理解”这个 ID (id: app-45) 为 (id: app: 45)

第二个问题是当我运行更新时,主干将 ID 发送到 0。

遵循请求的详细信息:

标头: 请求 URL:localhost(...) 请求方法:PUT 状态码:200 OK

请求有效载荷:

{"id":" app:120368", "title":"Test"}

预习:

预订:{ id:app:0 , title:test }

回复:

{"reservation":{"id":"app:0","title":"test"

4

1 回答 1

0

您始终可以覆盖模型的.parse()and.toJSON()方法来转换id值,例如

var Model = Backbone.Model.extend({
  parse: function(response){
    response.id = parseInt(response.id, 10); // or whatever function is appropriate
    return response;
  },
  toJSON: function(){
    var attrs = _.clone(this.attributes);
    attrs.id = "app-" + attrs.id; // or whatever reverses the transformation
    return attrs;
  }
})
于 2013-09-25T19:03:21.267 回答