流星.js单例确认框
Meteor.js mplement a singleton confirm box
我尝试创建一个可重用的确认框,但我不确定如何以流星的方式实现它。
我有一个确认框的模板。文本和按钮值应该是动态的。
<template name="confirm">
{{#if show}}
{{text}}
<button class="cancel">cancel</button>
<button class="confirm">{{action}}</button>
{{/if}}
</template>
我有一个带有删除按钮的用户模板。
<template name="user">
<h1>{{name}}</h1>
<button class="delete">delete user</button>
</template>
在应用程序模板中,我显示用户列表并呈现确认模板。
<template app="app">
{{#each user}}
{{> user}}
{{/each}}
{{> confirm}}
</tempalte>
现在,当我单击用户项的删除按钮时,我想显示确认框。
Template.confirm.helpers({
text: function(){
return Session.get('confirmText');
},
action: function(){
return Session.get('confirmAction');
},
show: function(){
return Session.get('showConfirm');
},
});
Template.user.events({
'click .delete': function(){
Session.set('confirmAction', 'delete');
Session.set('confirmText', 'Are you sure?');
Session.set('showConfirm', true);
}
});
我的确认框显示正常,但如何从确认框中触发用户删除?
我甚至走在正确的轨道上吗?我尝试在每个用户模板中呈现一个确认模板,但一次应该只有一个活动的确认框。
你当然可以使用这种模式让它工作。您唯一需要添加的是设置要在会话中删除的用户 ID,以便您的删除方法可以访问它:
Template.user.events({
'click .delete': function(){
Session.set('confirmAction', 'delete');
Session.set('confirmText', 'Are you sure?');
Session.set('showConfirm', true);
/* addition - this._id refers to the id of the user in this template instance */
Session.set('userToDelete', this._id);
}
});
然后:
Template.confirm.events({
"click button.confirm": function(){
Meteor.call(
"deleteUser",
Session.get("userToDelete"),
function(error, result){
Session.set("userToDelete", null);
}
);
}
});
但是,更灵活且可扩展的模式是使用附加到模板实例的ReactiveVar
或ReactiveDict
来获取和设置要确认删除的用户,以内部连接到用户模板。这样,您就不会使用实际上只涉及一个行为的键加载全局会话对象。您可以在其他不相关的上下文中重复使用confirm
模板。
更新
这是一种使用私有反应变量跨上下文重用确认按钮的方法。要查看另一个确认框是否打开,您可以先检查会话属性。
Session.setDefault("confirming", false);
confirm
模板中的text
和action
属性是从其用户父级设置的:
<template app="app">
{{#each user}}
{{> user}}
{{/each}}
</template>
<template name="user">
<h1>{{name}}</h1>
<button class="delete">delete user</button>
{{#if show}}
{{> confirm text=text action=action}}
{{/if}}
</template>
<template name="confirm">
{{text}}
<button class="cancel">cancel</button>
<button class="confirm">{{action}}</button>
</template>
我们还在用户父级中为其设置了帮助程序和事件:
Template.user.created = function(){
this.show = new ReactiveVar(false);
}
Template.user.helpers({
name: function(){
return this.name;
},
show: function(){
return Template.instance().show.get();
},
text: function(){
return "Are you sure?";
},
action: function(){
return "delete user";
}
});
Template.user.events({
"click button.delete": function(event, template){
if (Session.get("confirming")){
console.log("You are already confirming another deletion.");
return;
}
Session.set("confirming", true);
template.show.set(true);
},
"click button.confirm": function(event, template){
Meteor.call(
"deleteUser",
this._id,
function(error, result){
template.show.set(false);
Session.set("confirming", false);
}
)
}
});
现在,您可以根据confirm
模板的父级在其他地方为模板提供不同的上下文。
相关文章:
- 和兼容的单例骨干模型
- 有状态单例模块
- 在什么情况下我们需要在javsacript中实现单例类
- 确认单选按钮选择
- 如何将一个单例传递给另一个对象,使该对象的所有实例都引用同一个单实例
- 正在寻找特定的JS方法来确认单选按钮的选择
- 使用Ext.Ajax.request使ExtJS单例返回对象
- 原始值被称为单例的现象是什么?
- AngularJS 模式单例在服务中未更新视图
- 创建命名空间 Javascript 单例类的对象
- 流星.js单例确认框
- Javascript 单例模式
- 如何将 AMD 模块从单例转换为实例
- 为什么这个javascript单例方法有效
- 扩展现有单例
- JavaScript 对象的单例行为
- ES2015 单例或服务提供商或模块,用于敲除.js组件
- 如何在 chrome 开发者控制台中查看在 DOJO 中创建的单例对象
- 在单例模式中引用变量的根父级
- XHR 上传功能的单例引用不起作用