在Immutable.js中解析嵌套记录 [英] Parsing nested Records in Immutable.js

查看:106
本文介绍了在Immutable.js中解析嵌套记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我使用Immutable.js定义了以下记录:

Suppose I have the following Records defined using Immutable.js:

var Address = Immutable.Record({street: '', city: '', zip: ''});
var User = Immutable.Record({name: '', address: new Address()});

如何将普通javascript对象转换为用户记录?我尝试了以下但它没有产生预期的输出:

How do I convert plain javascript object into the User record? I tried the following but it does not produce the expected output:

var user = new User({name: 'Foo', address: {street: 'Bar', city: 'Baz'}});
// => Record { "name": "Foo", "address": [object Object] }

我是意识到可以显式创建地址记录:

I am aware that it is possible to explicitly create the Address record:

var user = new User({name: 'Foo', address: new Address({street: 'Bar', city: 'Baz'})});
// => Record { "name": "Foo", "address": Record { "street": "Bar", "city": "Baz", "zip": "" } }

但这不是我想要的解决方案。想象一下,你有记录嵌套了几个级别,并希望以JSON形式存储/检索数据(例如在数据库中)。我想使用实际的用户记录结构作为重新创建嵌套记录的模式信息。或者有更好的方法来表示嵌套和结构化的不可变数据吗?

But that is not solution I am looking for. Imagine you have Records nested several levels deep and want to store/retrieve the data as JSON (e.g. in database). I would like to use the actual User record structure as a schema information for recreating the nested records. Or is there a better way to represent nested and structured immutable data?

推荐答案

Record结构的预期用途是不验证提供数据的结构,只是为了确定允许的密钥集,如果没有给出,则提供默认值。

The intended use of Record structure isn't to verify the structure of provided data, just to determine set of keys that are allowed and provide default values if they're not given.

所以使用你的例子,如果你初始化记录没有提供地址,你将得到正确的地址的Immutable.Record对象:

So using your example, if you initialize the record without providing Address, you will get the proper Immutable.Record object for Address:

var user = new User({name: 'Foo'});
// => Record { "name": "Foo", "address": Record { "street": "", "city": "", "zip": "" } }

实现你想要的一种 hackish 方法是在 Immutable.fromJS上写一个包装器自定义 reviver 函数的方法:

One hackish way to achieve what you want would be to write a wrapper on Immutable.fromJS method with custom reviver function:

Immutable.Record.constructor.prototype.fromJS = function(values) {
  var that = this;
  var nested = Immutable.fromJS(values, function(key, value){
    if(that.prototype[key] && that.prototype[key].constructor.prototype instanceof Immutable.Record){return that.prototype[key].constructor.fromJS(value)}
    else { return value }
  });
  return this(nested);
}

然后你就可以这样使用它:

Then you can use it like this:

var user = User.fromJS({name: 'Foo', address: {street: 'Bar', city: 'Baz'}});

// => User { "name": "Foo", "address": Record { "street": "Bar", "city": "Baz", "zip": "" } }

但是如果你想正确检查你的数据结构,我建议你使用Immutable.js和一些静态类型检查器,比如 http://flowtype.org/ http://www.typescriptlang.org/

However if you want to have proper checking your data structures, I would recommend using Immutable.js together with some static type-checker, like http://flowtype.org/ or http://www.typescriptlang.org/

这篇关于在Immutable.js中解析嵌套记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆