Meteor

From Code Self Study Wiki
Jump to: navigation, search

Meteor.js notes.

Structure[edit]

Sample file structure:

$ tree . 
.
├── client
│   ├── common.js
│   ├── stylesheets
│   │   └── faq.css
│   └── templates
│       ├── head.html
│       ├── layout.html
│       └── modelname
│           ├── modelname.html
│           └── modelname.js
├── lib
│   ├── collections.js
│   └── router.js
├── public
│   └── img
├── README.md
└── server
    └── common.js

Security[edit]

$ meteor remove insecure autopublish

Publish / Subscribe[edit]

After removing autopublish, you need to manually publish and subscribe.

// Double-check if this syntax is correct
if (Meteor.isServer) {
    if (!userId) {
        // not logged in
    } else {
        return MyThings.find({userId: this.userId});
    }
}
 
if (Meteor.isClient) {
    meteor.Subscribe('mythings');
}

Database[edit]

Reset Database[edit]

$ meteor reset

Mongo Console[edit]

$ meteor mongo

Example query:

db.modelName.find().pretty()

Templating[edit]

Spacebars[edit]

Partials:

{{> myPartial}}
 
<template name="myPartial">
<h1>Saluton, Mondo!</h1>
</template>

Variables:

{{myVariable}}

Conditionals are similar to in Handlebars.js.

Authentication[edit]

$ meteor add accounts-ui accounts-password

Login and Registration[edit]

{{> loginbuttons}}
 
{{#if currentUser}}
    You are logged in
{{else}}
    You are not logged in
{{/if}}
// client/common.js
Meteor.startup(function () {
    AccountsEntry.config({
        homeRoute: '/', // mandatory - path to redirect to after sign-out
        dashboardRoute: '/',
        passwordSignupFields: 'USERNAME_AND_EMAIL'
    });
 
    Accounts.ui.config({
        passwordSignupFields: 'USERNAME_AND_EMAIL'
    });
});

Bootstrap example:

<ul class="nav navbar-nav navbar-right">
    {{#if currentUser}}
        <li><a href="profile">Edit Profile</a></li>
        <li><a href="sign-out">Logout</a></li>
    {{else}}
        <li><a href="sign-up">Sign Up</a></li>
        <li><a href="sign-in">Login</a></li>
    {{/if}}
</ul>

Restricting Access[edit]

Template.main.events({
    'submit .add-thing': function (e) {
        var text = e.target.text.value;
 
        // Calling a method from below
        Meteor.call('addThing', text);
        var text = '';
        return false;
    }
});
 
Meteor.methods({
    addThing: function (text) {
        if (!Meteor.userId()) {
            throw new Meteor.Error('not-authorized');
        }
        Things.insert({
            text: text,
            createdAt: new Date(),
            userId: Meteor.userId(),
            username: Meteor.user().username
        });
    },
    deleteThing: function (thingId) {
        Things.remove(thingId);
    },
    // Example of checking a box and saving that it's checked
    setChecked: function (thingId, setChecked) {
        var thing = Things.findOne(thingId);
        if (thing.userId !== Meteor.userId()) {
            throw new Meteor.Error('not-authorized');
        }
        Things.update(thingId, {$set: {checked: setChecked}});
    }
});

Resources[edit]