我怎样才能创build一个dynamic辅助函数的数据库查询
我有一个类别列表,我需要从我的应用程序中的许多位置的数据库中select。
到目前为止,我一直在查询的每一个使用后的callback链。
我只想在一个位置抓住它,所以如果我需要修改它,它就会干掉。
基本上:
function() { var categoryList = {}; var Category = Parse.Object.extend("Category"); var categoryQuery = new Parse.Query(Category); categoryQuery.find(function(categories) { categories.forEach(function(item) { item=item.toJSON(); categoryList[item.objectId] = item.Name; }); }); return categoryList; }
但是我不确定把它放在哪里,我意识到在那里写的categoryList将是空的。 我如何创build一个帮助函数,为我提供可以在任何地方使用的结果?
我想我可以把它放在一个外部文件,并使用要求,但我试过:
module.exports = {}; module.exports = function(fn) { <code> fn['categoryList'] = categoryList; return fn; }(module.exports);
而这似乎并没有工作。 我把这个要求放在app.js中,每次被包含,parsing就会说“更新失败,无法加载触发器”。
我很困难,而且比较新的节点/parsing。 谁能帮忙?
谢谢。
选项1
helper.js
var lib = require('lib'); // exports functions exports.help_a = function (...) { }; exports.help_b = function (...) { };
app.js
// imports heplers functions helper = require('./helper.js'); //call helper.help_a(...); helper.help_a(...);
选项2
helper.js
// exports module as function module.exports = function (...) { };
app.js
helper = require('./helper.js'); helper(...);