【问题标题】:Meteor 0.8.0, UI Expected valid attribute nameMeteor 0.8.0,UI 预期的有效属性名称
【发布时间】:2014-05-23 02:49:01
【问题描述】:

这是什么原因造成的?我检查了我的数据上下文,这实际上传递了适当的数据,这在之前版本的 Meteor 中有效。

Meteor UI 中的异常:错误:预期的有效属性名称、''、null 或对象

HTML:

    <select class='form-control' id='region' name="region" value="{{region}}">
      {{#each regions}}
        <option class="controls" value='{{_id}}' {{curRegion ../regionId}}>{{title}}</option>
      {{/each}}
    </select>

辅助函数:

Template.labEdit.helpers
  curRegion:  (region) ->
    console.log "Region: ", region, this
    return region is this._id && 'selected'

完整的堆栈跟踪:

Exception in Meteor UI: Error: Expected valid attribute name, '', null, or object
    at Object.Spacebars.attrMustache (http://localhost:3000/packages/spacebars.js?5d478ab1c940b6f5a88f78b8adc81a47f022da77:137:11)
    at HTML.OPTION.$dynamic (http://localhost:3000/client/views/labs/template.labEdit.js?b7b4fc16112efc0d900b152a545a4279764a2728:61:26)
    at Object.HTML.evaluateAttributes (http://localhost:3000/packages/htmljs.js?697b0dd0fbdd1f8984dffa3225121a9b7d0b8609:347:21)
    at http://localhost:3000/packages/ui.js?b523ef986d3d39671bcb40319d0df8982acacfe8:2351:28
    at callWithNoYieldsAllowed (http://localhost:3000/packages/deps.js?7afb832ce6e6c89421fa70dc066201f16f9b9105:74:5)
    at _.extend._compute (http://localhost:3000/packages/deps.js?7afb832ce6e6c89421fa70dc066201f16f9b9105:212:7)
    at new Deps.Computation (http://localhost:3000/packages/deps.js?7afb832ce6e6c89421fa70dc066201f16f9b9105:144:10)
    at Object._.extend.autorun (http://localhost:3000/packages/deps.js?7afb832ce6e6c89421fa70dc066201f16f9b9105:361:13)
    at materialize (http://localhost:3000/packages/ui.js?b523ef986d3d39671bcb40319d0df8982acacfe8:2346:30)
    at materialize (http://localhost:3000/packages/ui.js?b523ef986d3d39671bcb40319d0df8982acacfe8:2293:7) 

这是编译的 blaze 中实际抛出错误的部分:

UI.block(function() {
    var self = this;
    return [ "\n                ", HTML.OPTION({
      "class": "controls",
      $dynamic: [ function() {
        return Spacebars.attrMustache(self.lookup("state"), Spacebars.dot(self.lookup(".."), "status"));
      } ]
    }, function() {
      return Spacebars.mustache(self.lookup("."));
    }), "\n              " ];
  })), "\n            "), "\n          "), "\n          ", HTML.DIV({
    "class": "col-md-4"
  }, "\n            ", Spacebars.With(function() {

【问题讨论】:

  • 有趣的是,即使我得到了这些异常,一切似乎都正常。很奇怪……
  • 我相信这与{{curRegion ../regionId}} 部分有关。那个助手做什么/返回什么?
  • 基本上它是一个帮助器,它获取父数据上下文项 regionId,并将其与 this._id 进行比较,其中 'this' 是 {{#each}} 迭代的当前对象。如果 ID 匹配,它会返回一个 html 标记属性 'selected' 以在下拉列表中选择正确的项目。这可行,但以某种方式抛出错误。

标签: javascript coffeescript meteor meteor-blaze


【解决方案1】:

The way to use valueless attributes like selected has changed in Blaze.

因此,您可以将 curRegion 助手替换为 isRegion 助手:

Template.labEdit.helpers
  isRegion: (region) -> @_id is region

并将 HTML 行更改为:

<option class="controls" value='{{_id}}' selected={{isRegion ../regionId}}>{{title}}</option>

在我看来,它还使辅助函数和辅助调用更具可读性(和灵活性)。

【讨论】:

  • 哇不错的收获。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多