简体   繁体   中英

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

I was redirected here from the Meteor.com website. 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. Unfortunately, the tutorials offered by Meteor.com and the actual code that currently downloads are completely different. 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:

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

The client side main.js file looks like this:

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:

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. It's on the server of course, but it also runs on the browser in something called, 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?

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

The imports directory isn't 'eagerly' loaded (which means that meteor ignores files in it unless they are specifically imported).

Your line of code that defines the Mongo Collection would be in the imports/api/tasks/tasks.js file

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:

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

Here is a great article from TheMeteorChef that explains the imports directory well!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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