如何从 Meteorjs 中的多个文件访问集合? [英] How do I access a collection from more than one file in Meteorjs?

查看:20
本文介绍了如何从 Meteorjs 中的多个文件访问集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的主 js 文件中,我定义了一个new Meteor.Collection("Decks")",如果没有数据,我会填充一些数据.

In my main js file I have defined a "new Meteor.Collection("Decks")" and I populate some data if there is no data.

现在我在客户端文件夹中有一个视图文件夹,其中包含用于模板事件的 javascript.每个模板都有自己的 js 文件.这些事件需要查询和更新 Decks 集合,但我不知道该怎么做.我尝试在事件代码中编写一个new Meteor.Collection("Decks")",但我收到一个错误,该集合已经存在.

Now I have a views folder in the client folder that has the javascript for events of the templates. Each template has it's own js file. Those events need to query and update the Decks collection, but I have no idea how I would do that. I tried writing a "new Meteor.Collection("Decks")" in the event code, but I got an error that the collection already exists.

我可以想象使用 requirejs 将全局变量传递给事件,但我觉得必须有一种在 Meteor 中实现的本地方法.

I could imagine using requirejs to pass along a global variable to the events perhaps, but I feel there must be a native way to do it in Meteor.

如何访问不同文件中的同一个集合?

How do I access the same collection in a different file?

举个例子,我的应用叫做builder",所以主js叫做builder.js",包含:

As an example, my app is called "builder", so the main js is called "builder.js" and contains:

var Decks = new Meteor.Collection("Decks");

然后我在客户端文件夹中有另一个名为do_other_stuff.js"的 javascript 文件,它想查询 Decks 集合,例如:

Then I have another javascript file called "do_other_stuff.js" in the client folder and it wants to query the Decks collection, so something like:

var first_deck = Decks.findOne({name: "first"});

但显然这行不通,因为 Decks 不是全局变量.

But obviously that won't work, because Decks is not a global variable.

推荐答案

简单的,让 Decks 成为一个全局变量!删除 var 关键字:

Simply, make Decks a global variable! Remove the var keyword:

Decks = new Meteor.Collection("Decks");

这篇关于如何从 Meteorjs 中的多个文件访问集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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