1

meteor js で autoform 用に独自のカスタム入力タイプを作成しています。すべてが正常に機能しますが、ブラウザーのコンソールに奇妙なエラーが表示されます。このカスタム入力は、他のブートストラップ ドロップダウンにネストされる可能性のあるブートストラップ ドロップダウン マルチ チェックボックスです。ドロップダウン内のフィールドをチェックするとエラーが発生します。

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'}]

すべてが正常で、エラーはありません。

4

1 に答える 1

0

問題を解決しました。問題は非常に単純でしたが、javascript (および流星) がエラーを表示する方法のおかげで、フォーム内にフォームをネストしようとしたことに気づきませんでした。これが、「Uncaught Error: There is no current view」が発生した理由です。

完全に不意を突かれたのは、Chrome コンソールにエラーが表示された瞬間でした。「options」プロパティがこのような静的データで生成されている場合、フォームタグ内でオートフォームおよびネストされたフォームタグを使用すると、Meteor はエラーで文句を言いません。

[{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 はエラーをスローします。

于 2015-03-18T09:04:21.110 に答える