我正在为流星 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'}]
一切正常,没有错误。