【问题标题】:KnockoutJS: Binding to objects with illegal namesKnockoutJS:绑定到具有非法名称的对象
【发布时间】:2012-10-11 04:10:04
【问题描述】:

我正在处理的项目需要对有些复杂的 XML 文件进行大量工作,因此我将它们转换为 SimpleXML 数组,对其进行 JSON 编码并通过 Ajax 将其传递给 Knockout。

问题在于几乎每个属性都有属性,SimpleXML 将这些属性推入一个名为@attributes 的对象中。当我尝试像myObject().@attributes.Name 那样绑定它时,我得到一个错误

Message: SyntaxError: Unexpected token ILLEGAL;
Bindings value: text: alertObj().@attributes.Name 

我尝试了'@attributes', ['@attributes'], [@attributes],但没有任何效果。我也尝试过各种转义字符和 unicode 转义。

这是 json 的样子:

        var alertObject =
            {
                "@attributes":
                    {
                        "DescriptionContentType":"text\/plain",
                        "Description":"",
                        "IsActive":"true",
                        "Name":"Apache Requests Per Interval"
                    },
                "Frequency":
                    {
                        "PeriodInSeconds":"60"
                    },
                "MetricLevelNotification":"false",
                "AlertTriggerMode":"2",
                "CautionActionDelay":"0",
                "DangerActionDelay":"0",
                "CautionActionList":
                    {
                        "ActionID":
                            {
                                "ManagementModuleName":"Base Module",
                                "ConstructName":"Base SMTP Mail Action"
                            }
                    },
                "DangerActionList":
                    {
                        "ActionID":
                            {
                                "ManagementModuleName":"Base Module",
                                "ConstructName":"Base SMTP Mail Action"
                            }
                    },
                "MetricGroupingID":
                    {
                        "ManagementModuleName":"Base Module",
                        "ConstructName":"Apache Requests Per Interval"
                    },
                "AlertCombineOperator":"1",
                "AlertCompareOperator":"2",
                "CautionTargetValue":"2000",
                "DangerTargetValue":"15000",
                "CautionMinNumPerPeriod":"3",
                "CautionAlertPeriod":"6",
                "DangerMinNumPerPeriod":"1",
                "DangerAlertPeriod":"1"
            };

我想避免以任何方式改变结构和名称。

我怎样才能让它工作?

【问题讨论】:

    标签: javascript jquery xml json knockout.js


    【解决方案1】:

    那是因为它不是一个有效的 javascript 变量名:

    标识符必须以 $、_ 或 Unicode 中的任何字符开头 类别“大写字母(Lu)”,“小写字母(Ll)”, “大写字母 (Lt)”、“修饰字母 (Lm)”、“其他字母 (Lo)”、 或“字母编号 (Nl)”。

    来自http://mathiasbynens.be/notes/javascript-identifiers

    变量名不能以 @ 开头,因此您会看到错误。附加单引号、括号或其他任何东西没有意义。变量名根本无效,您需要更改从服务器返回数据的方式。

    【讨论】:

    • 这是有道理的。我将在 json 编码之前更改服务器端的数组。谢谢。
    【解决方案2】:

    我遇到了类似的问题,我完全同意您绝对应该更改从服务器返回数据的方式。但是,如果您真的无法控制数据或暂时无法影响它,您也许可以使用您想要的渲染:

    $data['@attributes'].Name
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      • 2018-10-26
      • 2013-08-03
      • 1970-01-01
      相关资源
      最近更新 更多