[英]How can I export from a Meteor package into my app's namespace?
I know how to write Meteor packages but I can't seem to figure out how to have all exports land in my app's namespace, as described in this presentation . 我知道如何编写 Meteor包,但我似乎无法弄清楚如何让所有导出都落在我的应用程序的命名空间中,如本演示文稿中所述 。
This particular package is specific to an app I'm building, and it exports only one method that can be regarded as a decorator on the app's singleton. 这个特定的包特定于我正在构建的应用程序,它只导出一个可以被视为应用程序单例的装饰器的方法。 I tried
api.export('MyApp.myMethod')
but that gives an error native: Bad exported symbol: MyApp.myMethod
. 我尝试了
api.export('MyApp.myMethod')
但是它给出了一个错误native: Bad exported symbol: MyApp.myMethod
。
If I just api.export('myMethod')
, then in the app code I have to call myMethod()
, and that's not namespaced. 如果我只是
api.export('myMethod')
,那么在应用程序代码中我必须调用myMethod()
,而不是命名空间。
Does Meteor have a mechanism similar to Node's var http = require('http');
Meteor是否具有类似于Node的
var http = require('http');
? ? Or how can packages export symbols into a given namespace?
或者如何将包导出到给定的命名空间?
The api.export
method is only supported for top-level variables right now . api.export
方法目前仅支持顶级变量 。 It doesn't work for nested variables, because " it turned out that using deep exports was very confusing ", and what would you expect MyApp.myMethod
to appear as in the global namespace if you also exported MyApp.myOtherMethod
? 它不适用于嵌套变量,因为“ 事实证明使用深度导出非常混乱 ”,如果你还导出了
MyApp.myOtherMethod
,你会期望MyApp.myMethod
在全局命名空间中出现?
You should just export MyPackage
, and then call MyPackage.myMethod()
. 您应该只导出
MyPackage
,然后调用MyPackage.myMethod()
。 A common approach is to do something like 一种常见的方法是做类似的事情
MyPackage = {
myMethod: someSecretMethodName,
myOtherMethod: otherMethodName
}
And then call api.export("MyPackage")
. 然后调用
api.export("MyPackage")
。 This means that the exported names of variables don't necessarily have to be what you called them. 这意味着变量的导出名称不一定必须是您所称的变量名称。 This is used a lot in the core meteor packages;
这在核心流星包中经常使用; you can also see an example at for MongoInternals in mongo_driver.js .
您还可以在mongo_driver.js中看到MongoInternals的示例 。
All properties that you register in your app namespace are made available for the packages that depend on (use) your app-package. 您在应用程序命名空间中注册的所有属性都可用于依赖(使用)您的app-package的软件包。 So, when you want to register a package namespace in an app-namespace, you declare the dependency on the app-package within your package and you register all of the methods/objects you want to export in the app-namespace.
因此,当您想在app-namespace中注册一个包命名空间时,您可以在包中声明app-package的依赖关系,并在app-namespace中注册要导出的所有方法/对象。 An example:
一个例子:
file: packages/myapp/namespace.js file:packages / myapp / namespace.js
MyApp = {};
file: packages/myapp/package.js file:packages / myapp / package.js
Package.on_use(function (api, where) {
api.add_files([
"namespace.js"
], ['client', 'server']);
api.export("MyApp", ['client', 'server']);
});
file: packages/myapp-module1/logic.js file:packages / myapp-module1 / logic.js
packageSpecificMethod = function(){}
moduleOne = {};
//you can export an module-specific namespace by registering it in the app-namespace
MyApp.module1 = moduleOne;
//or you can (if you dont want package-namespaces) register you private methods in the app-namespace directly
MyApp.exportedMethod = packageSpecificMethod;
file: packages/myapp-module1/package.js file:packages / myapp-module1 / package.js
Package.on_use(function (api, where) {
api.use([
'myapp'
], ['client', 'server']);
api.add_files([
"logic.js"
], ['client', 'server']);
});
In your package, you should define all methods and symbols in the namespace you want them to have, and then export that namespace. 在您的包中,您应该在您希望它们具有的命名空间中定义所有方法和符号,然后导出该命名空间。 So, if in your package you've got:
所以,如果在你的包装中你有:
MyApp = {
myMethod: ...
};
Then you export it with api.export('MyApp')
. 然后使用
api.export('MyApp')
导出它。
Unfortunately, there's no method similar to the one in Node you've mentioned, as all packages are loaded globally on startup. 不幸的是,没有类似于你提到的Node的方法,因为所有包都是在启动时全局加载的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.