简体   繁体   English

我把这行代码放在 Meteor 的新文件结构上的什么地方?

[英]Where do I put this line of code on the new file structure of Meteor?

I was redirected here from the Meteor.com website.我是从 Meteor.com 网站重定向到这里的。 I am new to this Javascript library and while I have a conceptual framework of how it works I wanted to get some real world experience with it.我是这个 Javascript 库的新手,虽然我有一个关于它如何工作的概念框架,但我想获得一些真实世界的经验。 Unfortunately, the tutorials offered by Meteor.com and the actual code that currently downloads are completely different.不幸的是,Meteor.com 提供的教程与当前下载的实际代码完全不同。 It looks like originally, all the js was on one file and now the server function and the rest of the js is broken up into two separate files and I have been unable to find documentation that would guide me towards which one of the two new js files do I add this piece of code to create my first collections:最初看起来,所有 js 都在一个文件中,现在服务器功能和 js 的其余部分被分解为两个单独的文件,我一直无法找到可以指导我使用两个新 js 中的哪一个的文档文件我添加这段代码来创建我的第一个集合:

Tasks = new.Mongo.Collection('tasks')

The client side main.js file looks like this:客户端 main.js 文件如下所示:

import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';

import './main.html';

Template.hello.onCreated(function helloOnCreated() {
  // counter starts at 0
  this.counter = new ReactiveVar(0);
});

Template.hello.helpers({
  counter() {
    return Template.instance().counter.get();
  },
});

// templates can have helpers which are just functions and events and this
// particular event is a click event
Template.hello.events({
  'click button'(event, instance) {
    // increment the counter when button is clicked
    instance.counter.set(instance.counter.get() + 1);
  },
});

The server side main.js file:服务器端 main.js 文件:

import { Meteor } from 'meteor/meteor';

Meteor.startup(() => {
  // code to run on server at startup
});

Now with Meteor.js we cannot assume that it would go on the server side because the database can be accessed from everywhere on the front-end and the back-end.现在有了 Meteor.js,我们不能假设它会在服务器端运行,因为可以从前端和后端的任何地方访问数据库。 It's on the server of course, but it also runs on the browser in something called, Mini-Mongo.它当然在服务器上,但它也在浏览器上以称为 Mini-Mongo 的方式运行。 So does it: a) not matter in which js file I put this piece of code, or b) go on the server side as best practice?那么是这样吗:a) 我把这段代码放在哪个 js 文件中无关紧要,或者 b) 作为最佳实践放在服务器端?

This is what I am unclear about in the world of Meteor.这是我在流星世界里不清楚的地方。 Thanks guys.谢谢你们。

So, the current recommended way to structure a Meteor application is to use the 'imports' directory因此,当前推荐的构建 Meteor 应用程序的方法是使用“imports”目录

The imports directory isn't 'eagerly' loaded (which means that meteor ignores files in it unless they are specifically imported).导入目录不是“急切”加载的(这意味着meteor 会忽略其中的文件,除非它们是专门导入的)。

Your line of code that defines the Mongo Collection would be in the imports/api/tasks/tasks.js file您定义 Mongo 集合的代码行将位于imports/api/tasks/tasks.js文件中

Then whenever you reference the Tasks collection in your application (on either the client or server side) you import the collection to your file by doing this:然后,每当您在应用程序中(在客户端或服务器端)引用 Tasks 集合时,您都可以通过执行以下操作将集合导入到您的文件中:

import { Tasks } from '/imports/api/tasks/tasks';

Here is a great article from TheMeteorChef that explains the imports directory well! 这是 TheMeteorChef 的一篇很棒的文章,它很好地解释了导入目录!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM