简体   繁体   English

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

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

I'm creating my own custom input type for autoform in meteor js. 我正在为流星js创建自己的自定义输入类型以进行自动套准。 Everything works as it should, but I get a weird error in browser's console. 一切正常,但在浏览器控制台中出现一个奇怪的错误。 This custom input is a bootstrap dropdown multi checkbox with possibility of being nested in other bootstrap dropdowns. 此自定义输入是“引导程序下拉列表”多复选框,有可能嵌套在其他引导程序下拉列表中。 Error occurs when you check any field inside dropdown. 当您检查下拉列表中的任何字段时,都会发生错误。

Uncaught Error: There is no current view

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

Here is my coffee file used for custom input. 这是我用于自定义输入的咖啡文件。

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()

jade file below: 玉石文件如下:

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

Schema file I use: 我使用的架构文件:

  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'

EDITED: 编辑:

Error is caused by CampaignLocations array in schema which is used for i18n in meteor app. 错误是由流星应用中用于i18n的架构中的CampaignLocations数组引起的。 It's global variable, maybe someway it's changing meteor context (and this value) because it loads variable outside current template. 它是全局变量,也许某种程度上改变了流星上下文(和该值),因为它在当前模板之外加载变量。 If I return static value like this below: 如果我像下面这样返回静态值:

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

Everything is ok and there is no error. 一切都很好,没有错误。

I solved the problem. 我解决了问题。 The problem was very simple but "thanks" to the way that javascript (and meteor) shows errors I didn't notice that I tried to nest form inside form, this is why "Uncaught Error: There is no current view" occured. 问题非常简单,但是“感谢” javascript(和流星)显示错误的方式,但我没有注意到我试图将表单嵌套在表单中,这就是为什么发生“未捕获的错误:没有当前视图”的原因。

What caught me completely off guard was the moment when error appeared in my chrome console. 让我完全措手不及的是Chrome控制台出现错误的那一刻。 Meteor will not complain with errors when using autoform and nesting form tag inside form tag when "options" property is generated with static data like this 当像这样用静态数据生成“ options”属性时,在表单标签内使用自动套准和嵌套表单标签时,流星不会报错

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

But if you will use for example this kind of code inside options property: 但是,如果您将在options属性中使用这种代码,例如:

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

Which is using interpolation or string concatenation then Meteor will throw the error. 使用插值或字符串连接时,Meteor将抛出错误。

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

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