【问题标题】:store the name of a function in a JSON file and later be able to load and call it from within a script?将函数的名称存储在 JSON 文件中,然后能够从脚本中加载和调用它?
【发布时间】:2017-06-11 09:26:50
【问题描述】:

那个标题有点乱码,这可能是重复的,但我已经挖掘了一段时间。这一定很简单。这个问题的公认答案对我不起作用:How to declare and use the name of a function from a json object?

任务:我正在尝试将 Vis.js 时间线的设置数据外部化为 JSON 文件。数据集没有问题,除了函数引用“orderByID”和“visTemplate”之外的所有选项都没有问题。这些是我定义的函数,它们存在于我正在处理 JSON 数据的脚本中。

当我尝试使用 JSON 而不尝试转换它时,Vis.js 会抱怨。当我使用下面的代码尝试上述问题的答案时,我得到了图像中显示的错误。

这是在Electron 中,并且正在通过 index.html 中的脚本标记加载脚本。

我等待这个简单问题的单行答案,这个问题已经花费了很多时间来描述。 ????


console.log(' timelineOptions.order', timelineOptions.order);
console.log(' timelineOptions.template', timelineOptions.template);
console.log('this', this);
console.log('window', window);

timelineOptions.order = window[timelineOptions.orderByID];
timelineOptions.template = window[timelineOptions.visTemplate];


 "timelineOptions": {
    "order": "orderByID",
    "selectable": true,
    "zoomable": false,
    "width": "100%",
    "height": "90%",
    "minHeight": 700,
    "format": {
        "minorLabels": {
            "hour": "HH\\h"
        }
    },
    "margin": {
        "axis": 20,
        "item": 20
    },
    "start": "2016-12-30",
    "end": "2017-01-4",
    "template": "visTemplate",
    "showCurrentTime": false,
    "dataAttributes": "all",
    "timeAxis": { "scale": "day", "step": 1 },
    "orientation": {
        "axis": "top",
        "item": "top"
    }
}

【问题讨论】:

    标签: javascript electron vis.js


    【解决方案1】:

    不确定您是否在窗口对象上设置了正确的引用,但您的代码不应该读取:

    timelineOptions.order = window[timelineOptions.order];
    

    您引用了字符串值 orderByID,而不是用于设置对象的属性名称。

    【讨论】:

    • 啊!!你的眼睛很好和/或我需要站起来四处走走!感谢您的协助!实际上,我查看了 window 对象的属性,并没有看到列出的我引用的函数 - 也许我也错过了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 2012-08-05
    • 2019-10-10
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多