使用来自其他脚本文件Meteor的脚本文件的正确方法是什么? [英] What's the correct way to use script files from other script files Meteor?

查看:142
本文介绍了使用来自其他脚本文件Meteor的脚本文件的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据和Meteor文档,Meteor将自动包含一个包文件夹结构中的所有脚本,并以最深的第一个递归。但是,当我在MyProject / sever文件夹中的脚本中定义js类时,如果从顶级.js文件引用,则该类是未定义的。如果我将类定义移动到 if(Meteor.isServer)上方的顶级.js文件中,则正确定义了类。在构造或包含外部文件的javascript时我缺少什么?

According to an answer to this question and the Meteor documentation, Meteor will automatically include all scripts in a package folder structure recursively with the deepest first. However, when I define a js class in a script in MyProject/sever folder the class is undefined if referenced from a top-level .js file. If I move the class definition to my top-level .js file above the if (Meteor.isServer) the class is defined correctly. What am I missing in structuring or including my javascript from external files?

编辑:
这个问题比仅仅看到的更为根本我的js课。我无法调用函数或在meteor 0.6创建的私有命名空间中看到任何。显然必须有一些方法来引用另一个文件中定义的东西,否则在流星中就不可能有大型项目。

The issue is more fundamental than just seeing my js class. I can't call a function or see anything in that private namespace that meteor 0.6 creates. Obviously there must be some way to reference stuff defined in another file or large projects wouldn't be possible in meteor.

推荐答案

As我怀疑,这与我对javascript(愚蠢的黑客)类声明和全局变量的误解有关。更改:

As I suspected, this was related to my misunderstanding of javascript's (goofy hack) class declarations and globals. Changing:

function ClassFoo(){
   this.bar = function(){}
}

ClassFoo = function ClassFoo(){
   this.bar = function(){}
}

通过使ClassFoo全局化来修复它。 FWIW,请注意 var ClassFoo = ... 将无效,因为它随后变为自动生成的闭包的命名空间的本地。一定要喜欢javascript的怪癖。

fixed it by making ClassFoo global. FWIW, note that var ClassFoo = ... will NOT work because it then becomes local to the auto-generated closure's namespace. Gotta love javascript's quirks.

这篇关于使用来自其他脚本文件Meteor的脚本文件的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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