Category Archives: Meteor

Meteor – Passing variables to template

In Meteor, a Spacebar template could include another template.

<template name="slots">
  {{> slot }}
</template>

<template name="slot">
  <div class="slot">
    <div class="number">100</div>
  </div>
</template>

 

Sometime we want to reuse the template with different variables. Here is an example.

<template name="slots">
  {{> slot one}}
  {{> slot two}}
  {{> slot three}}
</template>

<template name="slot">
  <div class="slot">
    <div class="number">{{number}}</div>
  </div>
</template>

Continue reading Meteor – Passing variables to template

Meteor – Run shell command at server side

1. Install the fibers node package on the server.

npm -g install fibers

 

2. Define the server methods in server.js.
server/server.js

Meteor.startup(function () {
  // Load future from fibers
  var Future = Npm.require("fibers/future");
  // Load exec
  var exec = Npm.require("child_process").exec;

  // Server methods
  Meteor.methods({
    runCode: function () {
      // This method call won't return immediately, it will wait for the
      // asynchronous code to finish, so we call unblock to allow this client
      // to queue other method calls (see Meteor docs)
      this.unblock();
      var future=new Future();
      var command="pwd";
      exec(command,function(error,stdout,stderr){
        if(error){
          console.log(error);
          throw new Meteor.Error(500,command+" failed");
        }
        future.return(stdout.toString());
      });
      return future.wait();
    }
  });
});

Continue reading Meteor – Run shell command at server side

Meteor – Distinct MongoDB Query

Meteor doesn’t support distinct in query. Say, i have a dataset as follow.

{
  "student":
  [
    { "_id": "1", "class": "1A", "name": "Peter" },
    { "_id": "2", "class": "1A", "name": "Jack" },
    { "_id": "3", "class": "1B", "name": "Mary" },
    { "_id": "4", "class": "1C", "name": "Sam" },
    { "_id": "5", "class": "1C", "name": "Ann" },
  ]
}

 

What i would like to know is how many distinct classes are there. Here is the solution suggested by Jonathan Pidgeon from Empire5Design.
Continue reading Meteor – Distinct MongoDB Query

Meteor – Insert Sample Data if the MongoDB is empty

Say we have a Meteor collection called Student.

model.js

Student = new Meteor.Collection('student');

 

So i would like to have some predefined students if the collection is emtpy. So i prepare the student.json in the private folder.

private/student.json

{
  "student":
  [
    { "_id": "1", "name": "Peter" },
    { "_id": "2", "name": "Jack" },
    { "_id": "3", "name": "Mary" },
    { "_id": "4", "name": "Sam" },
    { "_id": "5", "name": "Ann" },
  ]
}

Continue reading Meteor – Insert Sample Data if the MongoDB is empty

Meteor – High Network IO as it keeps polling MongoDB

I was working on a Meteor project which is connected to a mongoDB hosted on MongoLab. Since it is a sandbox plan, the oplog of the mongoDB instance is not available and in that case the Meteor will keep polling the mongoDB in around every 10 seconds. This causes a very high consumption on CPU and network bandwidth.

If you are running, Meteor 0.7.0 or above and your mongoDB has oplog enabled. You won’t experienced the problem.

Since the oplog is not available so the only workaround which i could think of is to unsubscribe the dataset. Here is a main.js which is executed when the app is loaded and when the user is idle for than 3 mins. Stop the subscription and that will stop polling the mongoDB.
Continue reading Meteor – High Network IO as it keeps polling MongoDB