清理我的流星函数和查询
Cleaning up my Meteor functions and queries
我有许多客户端函数,包括find()查询和日期/时间翻译,我已经在我的项目中的多个地方列出了这些函数。对于 Meteor,放置这些内容的最佳位置在哪里,以便我可以消除冗余和可能的不一致?
另外,我将如何引用,例如,find() 查询?会是这样吗?或者有没有更好的方法来做到这一点?
// JS file calling my re-usable function
Template.templatename.events({
'click .class': function {
var cats = function spaghetti();
return cats;
}
});
// JS file with re-usable functions
function spaghetti() {
return Collection.find();
}
是否确实
要在单击事件中返回集合?
如果您指的是帮助程序而不是事件,则可以执行以下操作:
/**
* Only on catsList template
*/
Template.catsList.helpers({
cats: function() {
return Cats.find();
}
})
/**
* On all templates
*/
Template.registerHelper('cats', function() {
return Cats.find();
});
我认为这就是您要找的,例如,如果您在 7 个不同的模板上有相同的Collection.find();
(例如),您可以从 meteor-templates-extension
使用此功能。
.HTML
<body>
{{> foo}}
{{> foo2}}
</body>
<template name="foo">
{{bar}}
<button type="button">Click</button>
</template>
<template name="foo2">
{{bar}} 2
<button type="button">Click 2</button>
</template>
<!-- and so on -->
.JS
Template.foo.helpers = function () {
thisHelperIsAvaibleEverywhere:function(){
return "Test";
}
};
Template.foo.events({
'click button': function (event, template) {
console.log("foo button clicked");
}
});
Template.foo2.inheritsHelpersFrom("thisHelperIsAvaibleEverywhere");
Template.foo2.inheritsEventsFrom("foo");
因此,现在两个模板Foo and Foo2
可以使用相同的帮助程序,并且它们将使用名为 foo
的相同事件
看看并告诉我是否有效
相关文章:
- 如何将参数赋予作为查询参数的参数值给定的函数
- j查询关于回调函数的术语
- 如何在函数带有更多postJSON查询时使用jQuery
- j查询如何在加载了 load() 函数的页面上进行 POST/AJAX,而无需刷新页面
- j查询限制文本函数
- j查询函数导入
- 如何委派查询函数
- j查询函数不在胡子中触发
- j查询函数在第一次点击时不触发
- j查询函数范围混淆
- 猫鼬,函数不能在未定义的、来自查询函数上执行
- j查询函数和自调用匿名函数之间的区别
- 编写mongodb REST API的通用查询函数
- 带有Select2的FormValidation未捕获异常:未为Select2定义查询函数
- 如何更好地优化这些数据库查询/函数流
- 不能在查询函数中访问自变量
- Javascript自执行查询函数
- j查询函数同步调用
- j单击时查询函数未被类多次工作
- “未为 Select2 未定义错误定义查询函数”