【发布时间】:2018-06-25 12:03:55
【问题描述】:
所以我在使用 Immutable.js 嵌套报告访问数据时遇到问题。
我似乎在这里by AndrewBestbier 找到了解决方案,但我无法将其实际应用到我的代码中。
解决方案说fromJS() 从 immutable.js 导入的函数接受“reviver”函数。然后它继续在这里提供某种包装函数:
Record.constructor.prototype.fromJS = function(values) {
var nested = fromJS(values, function(key, value){
//See https://facebook.github.io/immutable-js/docs/#/fromJS for docs on custom reviver functions
if(this.prototype[key] && this.prototype[key].constructor.prototype instanceof Record){
return this.prototype[key].constructor.fromJS(value.toJS()); //use toJS() here if nest more than once
}
else {
return value;
}
}.bind(this));
console.log()
return this(nested);
};
我不确定如何将它应用到我的代码中:
import { fromJS, mergeDeep, OrderedMap, Record } from 'immutable'
// Putting the suggested function here doesn't seem to do anything
// I must be missing some steps to get fromJS() to work with this
// wrapper code.
const mergeEntities = (state, payload) => {
return state.merge(payload.map( (id) => new Entity(id) ))
}
return mergeEntities(state, fromJS(action.payload.entities));
如果有人能指出我正确的方向,那就太好了!
编辑:
这是我导出的记录文件的摘录:
const EntityRecord = new Record({
id: undefined,
status: "",
messages: new Message(),
redirectTo: "",
entityType: "",
isFetching: undefined,
lang: "",
title: "",
charset: "",
viewport: "",
description: "",
keywords: "",
forms: new Form(),
socialLoginText: "",
forgotPasswordLinkText: "",
registerLinkText: ""
});
class Entity extends EntityRecord{
}
export default Entity
【问题讨论】:
标签: javascript ecmascript-6 immutable.js