MCh*_*han 5 javascript meteor meteor-autoform
我试图覆盖autoform-remove-item按钮的按钮单击事件,如下所示,因为我试图显示一条警告消息(之前),用户可以删除Autoform数组中的任何项目.然后,如果用户确认删除了项目,则按钮点击事件将继续正常.但是,在用户确认/拒绝删除之前,我无法弄清楚如何覆盖按钮的click事件以暂停其下面的代码(我无权访问)?有什么帮助我可能会在这里失踪吗?谢谢
Template.salesInvoice.events({
'click .autoform-remove-item': function(e){
e.preventDefault();
bootbox.dialog({
message: "Are you sure you wish to delete this item?",
title: "New Journal",
buttons: {
eraseRecord: {
label: "Yes!",
className: "btn-danger",
callback: function() {
}
},
doNotEraseRecord: {
label: "No!",
className: "btn-primary",
callback: function() {
//Continue with the normal button click event
}
}
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
我试图覆盖的click事件:
'click .autoform-remove-item': function autoFormClickRemoveItem(event, template) {
var self = this; // This type of button must be used within an afEachArrayItem block, so we know the context
event.preventDefault();
var name = self.arrayFieldName;
var minCount = self.minCount; // optional, overrides schema
var maxCount = self.maxCount; // optional, overrides schema
var index = self.index;
var data = template.data;
var formId = data && data.id;
var ss = AutoForm.getFormSchema(formId);
// remove the item we clicked
arrayTracker.removeFromFieldAtIndex(formId, name, index, ss, minCount, maxCount);
},
Run Code Online (Sandbox Code Playgroud)
在圈子里跑了一个小时后,这就是我发现的.Blaze将它的事件监听器安装在其父元素的内部templateInstance
.他们被置于财产之下的财产被称为$blaze_events
.它易于使用,但没有文档记录.以下是如何使用它:
处理程序存储在array($blaze_events.handlers
)中.所以要找到一个特定的我写这个函数:
retrieveBlazeEvent = function retrieveBlazeEvent (parentElement, eventType, selector) {
var returnHandler
parentElement.$blaze_events[eventType].handlers.forEach(function (eventHandler) {
if(eventHandler.selector === selector)
return (returnHandler = eventHandler) && false
})
return returnHandler
}
Run Code Online (Sandbox Code Playgroud)
用法:
客户机/ test.html中
<template name="test">
<p>Hi!</p>
</template>
Run Code Online (Sandbox Code Playgroud)
客户机/ main.html中
{{> test}}
客户机/ main.js
Template.test.events({
'click p': function () {
console.log('PIII')
}
})
retrieveBlazeEvent(document.body, 'click', 'p')
Run Code Online (Sandbox Code Playgroud)
返回的事件处理程序retrieveBlazeEvent
有一个函数unbind
.所以这是一个例子
retrieveBlazeEvent(document.body, 'click', 'p').unbind()
Run Code Online (Sandbox Code Playgroud)
调用此函数后,事件处理程序仍将存在.要恢复它,您只需调用bind
同一个对象即可.
假设我们有一个简单的p
元素.正在收听点击事件:
Template.test.events({
'click p': function () {
console.log('PIII')
}
})
Run Code Online (Sandbox Code Playgroud)
要触发事件处理程序,我们需要一个p
匹配此选择器的元素.我们还需要模板实例来检索正确的父节点.然后我们需要以视图作为其上下文来调用事件处理程序.我们还需要为包含的事件绑定创建存根currentTarget
.这个功能可以满足您的所有需求:
triggerEvent = function triggerEvent (eventType, elementSelector, context) {
var context = context || document
var element = context.querySelector(elementSelector)
var eventStub = {
currentTarget: element
}
var view = Blaze.getView(element)
retrieveBlazeEvent(view._domrange.parentElement, eventType, elementSelector)
.delegatedHandler
.call(view, eventStub)
return true
}
Run Code Online (Sandbox Code Playgroud)
小智 2
难道你不能在删除集合的代码中显示引导框确认吗?然后仅在用户确认时将其从集合中删除。例如,我认为这应该有帮助:
'click .autoform-remove-item': function autoFormClickRemoveItem(event, template) {
bootbox.dialog({
message: "Are you sure you wish to delete this item?",
title: "New Journal",
buttons: {
eraseRecord: {
label: "Yes!",
className: "btn-danger",
callback: function() {
var self = this;
var name = self.arrayFieldName;
var minCount = self.minCount; // optional, overrides schema
var maxCount = self.maxCount; // optional, overrides schema
var index = self.index;
var data = template.data;
var formId = data && data.id;
var ss = AutoForm.getFormSchema(formId);
arrayTracker.removeFromFieldAtIndex(formId, name, index, ss, minCount, maxCount);
}
},
doNotEraseRecord: {
label: "No!",
className: "btn-primary",
callback: function() {
//Continue with the normal button click event
}
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)