Meteor定制包中的CoffeeScript命名空间导出 [英] CoffeeScript namespace exports in Meteor custom packages

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

问题描述

几个小时,我无法获取Meteor包导出CoffeeScript文件中定义的变量。

For a few hours, I couldn't get my Meteor package to export variables defined in CoffeeScript files.

foo.coffee 中,例如,我尝试使用 @Foo = {foo :1} Foo = {foo:1} exports.Foo = {foo:1} ,等等,但是没有将工作。

In foo.coffee, for example, I tried using @Foo = {foo: 1}, Foo = {foo: 1}, exports.Foo = {foo: 1}, and so on and so forth, but nothing would work.

最后,查看Meteor coffeescript测试包在github ,我放置 api.export(); em> api.on_use()调用该包并且工作

Finally, after looking at the Meteor coffeescript test package on github, I placed the api.export(); call before the api.on_use() call for that package and it worked.

我的套件设定如下:

foo/
    .meteor/
    .build/
    foo.coffee
    package.js



foo.coffee



foo.coffee

class FooBar
  constructor: ->

Foo =
  FooBar: FooBar





package.js

Package.describe({
    summary: "A package that makes foo with foobar"
});

Package.on_use(function(api) {
    api.use("coffeescript", "client");
    api.export("Foo", "client"); // <-- Moved this to *before* the on_use declaration

    api.add_files("foo.coffee", "client");

});


推荐答案

Coffeescript编译

Coffeescript compiles

@Foo =
  FooBar: FooBar


$ b b

to

(function() {
  this.Foo = { 
    FooBar: FooBar
  };  
}).call(this);

您必须先移除 Foo ,请查看命名空间,但这不是一个好主意因为您可能需要在修改原始咖啡文件后对其进行编译。

You need to remove the this. before Foo, take a look at namespace, but it's not a good idea since you might need to compile it once you modify the original coffee files.

这里是我的秘诀:

名为 global_variables.js 的文件:

Foo = this.Foo;

然后将其添加到package.js中:

Then add it in your package.js:

api.add_files('xxx', 'xxx', 'global_variables.js');

然后就可以了!

这篇关于Meteor定制包中的CoffeeScript命名空间导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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