繁体   English   中英

Meteor JS Autoform自定义输入-没有当前视图

[英]Meteor JS Autoform Custom Input - There is no current view

我正在为流星js创建自己的自定义输入类型以进行自动套准。 一切正常,但在浏览器控制台中出现一个奇怪的错误。 此自定义输入是“引导程序下拉列表”多复选框,有可能嵌套在其他引导程序下拉列表中。 当您检查下拉列表中的任何字段时,都会发生错误。

Uncaught Error: There is no current view

Blaze._getCurrentView
Blaze.getView
AutoForm.templateInstanceForForm
_validateField
_.throttle.later

这是我用于自定义输入的咖啡文件。

AutoForm.addInputType "dropdownMultiCheckbox",
  template: "afDropdownMultiCheckbox"
  valueOut: () ->
    grabInput = $(@).children().children('input:checked')
    holder = []
    grabInput.each ->
      holder.push $(@).val()
    if $(grabInput[0]).hasClass('all-selector')
      holder.shift()
    holder

SimpleSchema.messages
  'atLeastOne': 'You need to select at least one field'

Template.afDropdownMultiCheckbox.helpers

  options: ->
    options = @.selectOptions
    options

  dsk: () ->
    @.atts["data-schema-key"]

Template.afDropdownMultiCheckbox.events
  'click div.dropdown-toggle': (event) ->
    $(event.target).siblings("ul.dropdown-menu").toggle()
  'click .all-selector': (event) ->
    if event.target.checked
      $(event.target).parent().siblings().children(".checkbox-options").prop('checked',true)
    else
      $(event.target).parent().siblings().children(".checkbox-options").prop('checked',false)
  'click .checkbox-options': (event,templateInstance) ->
    if !(event.target.checked)
      $(event.target).parent().siblings().children(".all-selector").prop('checked',false)
    if $(".check-onclick-#{@.class}:checked").length == $(".check-onclick-#{@.class}").length
      $("#checkbox-all-#{templateInstance.data.atts.id}").prop('checked',true)
  'click div.btn.btn-default.dropdown-toggle,ul,ul *': (event) ->
    event.stopPropagation()

Template.afDropdownMultiCheckbox.rendered = ->
  instanceOfTemplate = @
  $("*").on "click", (event) ->
    if !($(event.target)[0] == $(".class-#{instanceOfTemplate.data.atts.id}")[0] ||
       $(event.target)[0] == $("##{instanceOfTemplate.data.atts.id}")[0] ||
       $(event.target).hasClass("close-dropdown-multi"))
      $(".class-#{instanceOfTemplate.data.atts.id}").hide()

玉石文件如下:

template(name="afDropdownMultiCheckbox")
  .dropdown
    .btn.btn-default.dropdown-toggle(type="button", id="{{atts.id}}", aria-expanded="false")
      | {{atts.buttonText}}
      span.caret
    ul.dropdown-menu(role="menu", aria-labelledby="{{atts.id}}",class="class-{{atts.id}}")
      form
        div(data-schema-key="{{dsk}}")
          if atts.allOption.presence
            li.close-dropdown-multi(role="presentation")
              input.all-selector.close-dropdown-multi(type="checkbox", value="{{atts.allOption.value}}", id="checkbox-all-{{atts.id}}", role="menuItem")
              label.close-dropdown-multi(for="checkbox-all-{{atts.id}}") {{atts.allOption.value}}
          +each options
            li.close-dropdown-multi(role="presentation")
              input.close-dropdown-multi.checkbox-options(class="check-onclick-#{this.class}", role="menuItem", type="checkbox", value="#{this.text}", id="checkbox-#{this.text}")
              label.close-dropdown-multi(for="checkbox-#{this.text}") {{this.text}}
        br

我使用的架构文件:

  categories:
    type: [String]
    optional: false
    custom: ->
      if this.value.length == 0
        'atLeastOne'
    autoform:
      buttonText: 'Categories'
      label: false
      id: 'dropdown-nr-1'
      options: -> _.map CampaignCategories, (arg1) ->
        option =
          text: t "campaign.categories.#{arg1}"
          class: 'dropdown-vol-1'
      allOption:
        presence: false
        value: 'All'
      afFieldInput:
        type: 'dropdownMultiCheckbox'

  locations:
    type: [String]
    optional: false
    custom: ->
      if this.length == 0
        'atLeastOne'
    autoform:
      buttonText: 'Locations'
      label: false
      id: 'dropdown-nr-2'
      allOption:
        presence: true
        value: 'All'
      options: -> _.map CampaignLocations, (arg1) ->
        option =
          text: t "campaign.locations.#{arg1}"
          class: 'dropdown-vol-2'
      afFieldInput:
        type: 'dropdownMultiCheckbox'

编辑:

错误是由流星应用中用于i18n的架构中的CampaignLocations数组引起的。 它是全局变量,也许某种程度上改变了流星上下文(和该值),因为它在当前模板之外加载变量。 如果我像下面这样返回静态值:

[{text: 'test',class: 'test'},{text: 'test',class: 'test'},{text: 'test',class: 'test'}]

一切都很好,没有错误。

我解决了问题。 问题非常简单,但是“感谢” javascript(和流星)显示错误的方式,但我没有注意到我试图将表单嵌套在表单中,这就是为什么发生“未捕获的错误:没有当前视图”的原因。

让我完全措手不及的是Chrome控制台出现错误的那一刻。 当像这样用静态数据生成“ options”属性时,在表单标签内使用自动套准和嵌套表单标签时,流星不会报错

[{text: 'test',class: 'test'},{text: 'test',class: 'test'},{text: 'test',class: 'test'}]

但是,如果您将在options属性中使用这种代码,例如:

  options: -> _.map CampaignLocations, (arg1) ->
    option =
      text: t "campaign.locations.#{arg1}"
      class: 'dropdown-vol-2'

使用插值或字符串连接时,Meteor将抛出错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM