Meteor template default events

Whenever you create a new template you can use these boilerplate events and rename them to fit your needs.

Template.templateName.onDestroyed(function (){

});

Template.templateName.onRendered(function () {

});

Template.templateName.events({

});

Template.templateName.helpers({

});

How to import from JSON file into Meteor

    // import data only when Products collection is empty

    if (Products.find().count() === 0) {
        console.log("Importing private/products.json to db")

        var data = JSON.parse(Assets.getText("products.json"));

        data.forEach(function (item, index, array) {
            Products.insert(item);
        })
    }

 

Credits:
https://stackoverflow.com/questions/25370332/import-json-file-into-collection-in-server-code-on-startup

 

 

Meteor multi-tenancy patterns and package

Packages that can be used to create multi-tenant apps.

https://github.com/flipace/meteor-tenantify

https://github.com/mizzao/meteor-partitioner

This is a ripped down version of what’s in my main.js file in the client:

if (!Session.get('tenantId')) {
  var hostnameArray = document.location.hostname.split('.'), subdomain;
  if (hostnameArray[1] === 'mydomainname' && hostnameArray[2] === 'com') {
    subdomain = hostnameArray[0];  
  }
  if (subdomain) {
    Meteor.call('findTenantBySubdomain', subdomain, function(err, res) {
      var tenantId = res;
      if (tenantId) {
        Session.set('tenantId', tenantId); 
      }
    });
  }
}

It’s not pretty, but it works. Note: subdomains are unique to tenants, so we can get the tenant _id value by finding the right document by subdomain:

Meteor.methods({
  "findTenantBySubdomain" : function (subdomain) {
    check(subdomain, String);
    var tenant = Tenants.findOne({subdomain: subdomain});
    if (tenant) {
      return tenant._id;    
    }
  }
});

source: https://forums.meteor.com/t/multitenancy-and-meteor/5653/14

How to return a value with Meteor.call on client

Meteor.methods({
 test: function() { if (this.isSimulation) { 
return 1; 
// Client wants this to be 1 
} else {
 return 2; // Server wants this to be 2 
} } });
 Template.hello.events({
 'click button': function () {
 // Call the test method sync 
var clientResult = Meteor.apply('test', [], {returnStubValue: true});
 Session.set('result', clientResult); } });



See this thread for information.

https://forums.meteor.com/t/how-to-return-value-on-meteor-call-in-client/1277/14

 

 

Meteor multi-tenant architecture options

Looking into creating a multi-tenant app with Meteor?

Luckily you’re not alone and there are quite a few options available. We suggest you try reading the following links.

https://stackoverflow.com/questions/17356615/how-to-design-a-meteor-smart-package-to-transparently-separate-an-app-into-diffe

https://forums.meteor.com/t/multitenancy-and-meteor/5653/26

https://atmospherejs.com/mizzao/partitioner

Also, a code snippet is included below which can give you some inspiration on how you want to approach this design pattern.

Meteor.publish("data", function() {
 var tenantId = Meteor.users.findOne({_id: this.userId}).tenantId;
 return MyCollection.find({tenantId: tenantId}, {fields: {tenantId: 0}});
});

Meteor.http. Get Proper Example

 

if (Meteor.is_server) {
	Meteor.methods({
		fetchFromService: function(userName) {
			var url = "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name="+userName+"&count=10";
			//synchronous GET
			var result = Meteor.http.get(url, {timeout:30000});
			if(result.statusCode==200) {
				var respJson = JSON.parse(result.content);
				console.log("response received.");
				return respJson;
			} else {
				console.log("Response issue: ", result.statusCode);
				var errorJson = JSON.parse(result.content);
				throw new Meteor.Error(result.statusCode, errorJson.error);
			}
		}
	});
}

 

 

https://gist.github.com/nachiket-p/2922814