簡體   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