Ruby on rails 主干网不';当id为字符串时,不返回属性
我正在运行一些rails代码来生成json供主干使用。当我将id视为字符串,并在主干中使用它时,toJSON()函数不会返回属性。在id上调用a to_I时,toJSON()工作正常。(但这破坏了我的应用程序,因为“012345”与12345不同 我的主要观点是:Ruby on rails 主干网不';当id为字符串时,不返回属性,ruby-on-rails,backbone.js,Ruby On Rails,Backbone.js,我正在运行一些rails代码来生成json供主干使用。当我将id视为字符串,并在主干中使用它时,toJSON()函数不会返回属性。在id上调用a to_I时,toJSON()工作正常。(但这破坏了我的应用程序,因为“012345”与12345不同 我的主要观点是: serialize: -> console.log @model.toJSON() info: @model.toJSON().info 非工作json响应: {"id":"123456","inf
serialize: ->
console.log @model.toJSON()
info: @model.toJSON().info
非工作json响应:
{"id":"123456","info":[{"label":"Hire Date","text":"06-NOV-00"},{"label":"User ID","text":"YADDA"},{"label":"Employee Number","text":"123456"}] }
非工作结果:
data_partition: DataPartition
id: "123456"
__proto__: Object
工作json:
{"id":123456,"info":[{"label":"Hire Date","text":"06-NOV-00"},{"label":"User ID","text":"YADDA"},{"label":"Employee Number","text":123456}] }
正在处理JSON():
但是,当我删除领先的0时,这会破坏我的rails应用程序。运行此代码时,我没有发现任何问题:
var responseJSON_1 = {"id":"123456","info":[{"label":"Hire Date","text":"06-NOV-00"},{"label":"User ID","text":"YADDA"},{"label":"Employee Number","text":"123456"}] };
var responseJSON_2 = {"id":123456,"info":[{"label":"Hire Date","text":"06-NOV-00"},{"label":"User ID","text":"YADDA"},{"label":"Employee Number","text":"123456"}] };
var MyModel = Backbone.Model.extend({});
var myModel_1 = new MyModel( responseJSON_1 );
var myModel_2 = new MyModel( responseJSON_2 );
console.log( myModel_1.toJSON() );
console.log( myModel_2.toJSON() );
检查
您确定响应中更改的内容不会超过
id
格式吗?看起来像最右边的“文本”字段在工作的json中也会变得非字符串化。我怀疑前导零也会被截断。因此,是的,在给定的示例中,id的变化不止这些。我们从未发现这一点,但解决了javascript堆栈中的其他问题。最后,为了一致性,我们在所有地方都使用了整数,然后将其格式化为字符串h在我们需要它的地方。
var responseJSON_1 = {"id":"123456","info":[{"label":"Hire Date","text":"06-NOV-00"},{"label":"User ID","text":"YADDA"},{"label":"Employee Number","text":"123456"}] };
var responseJSON_2 = {"id":123456,"info":[{"label":"Hire Date","text":"06-NOV-00"},{"label":"User ID","text":"YADDA"},{"label":"Employee Number","text":"123456"}] };
var MyModel = Backbone.Model.extend({});
var myModel_1 = new MyModel( responseJSON_1 );
var myModel_2 = new MyModel( responseJSON_2 );
console.log( myModel_1.toJSON() );
console.log( myModel_2.toJSON() );