你如何在 Meteor 中管理命名空间? [英] How do you manage namespace in Meteor?

查看:55
本文介绍了你如何在 Meteor 中管理命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我的问题:目前,我在模板 js 文件中有十几个与 WEBRTC 相关的功能.我的目标是将这些函数放在一个单独的文件中,例如 webRTCWrapper.js,并在不使用全局变量的情况下在我的模板中调用这些函数.

So here is my problem : Currently, I have a dozen of functions related to WEBRTC within a template js file. My objective is to have those functions in a separate file, called webRTCWrapper.js for example, and to call those functions in my template without using global variable.

我想我必须使用命名空间,对吗?如果是这样,您如何使用它们?

I think I must use namespaces, am I correct ? If so, how do you use them ?

对于任何感兴趣的人,这正是我想要的:

EDIT : For anyone interested, this is exactly what I was looking for :

http://themeteorchef.com/snippets/using-the-module-pattern-with-meteor/

推荐答案

创建一个名为 packages/ 的目录与您的 .meteor/ 目录平行.您可以创建一个导出单个对象/函数的包.在命令行中,使用 meteor create --package meteor add 就可以编辑js文件添加命名空间了.

Make a directory called packages/ parallel to your .meteor/ directory. You can create a package that exports a single object/function. On the command line, use meteor create --package <yourpackagename> and meteor add <yourpackagename> You can edit the js file to add a namespace.

MyNamespace = {};
MyNamespace.myFunction = function () { };

然后,在 package.js 中,简单地导出该命名空间.

Then, in the package.js, simply export that namespace.

api.export('MyNamespace');

这篇关于你如何在 Meteor 中管理命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆