简体   繁体   English

骨干.js和字母ID的问题

[英]Issue with backbone.js and id with letters

I have a problem with Backbone.js. 我对Backbone.js有问题。 My system has inconsistency in the pattern of ID. 我的系统的ID模式不一致。

The fact is that the system has two types of ID: 事实是系统具有两种类型的ID:

1) id: 45 (integer, default and works properly) 2) id: app-45 (with problems) 1)id:45(整数,默认值,并且可以正常运行)2)id:app-45(有问题)

The first problem is that the backbone "understands" this ID (id: app-45) as (id: app: 45) 第一个问题是骨干网将这个ID(id:app-45)“理解”为(id:app:45)

The second problem is when I'll run an update, and the backbone sends the ID to 0. 第二个问题是何时运行更新,并且主干将ID发送到0。

Follows the details of the request: 遵循请求的详细信息:

Headers: Request URL: localhost(...) Request Method:PUT Status Code:200 OK 标头:请求URL:localhost(...)请求方法:PUT状态码:200 OK

Request payload: 请求有效负载:

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

Preview: 预习:

reservation: { id:app:0 , title:test } 保留:{ id:app:0 ,title:test}

Response: 响应:

{"reservation":{"id":"app:0","title":"test" {“预订”:{“ id”:“ app:0”,“标题”:“测试”

You could always override the .parse() and .toJSON() methods of your model to transform id values, eg 您总是可以覆盖模型的.parse().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;
  }
})

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

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