Skip to content

Instantly share code, notes, and snippets.

@acidsound
Created November 19, 2012 06:58
Show Gist options
  • Save acidsound/4109325 to your computer and use it in GitHub Desktop.
Save acidsound/4109325 to your computer and use it in GitHub Desktop.
publishing & subscribing multiple subsets of the same server collection in Meteor
<head>
<title>multiView</title>
</head>
<body>
{{> hello}}
</body>
<template name="hello">
<h3>ViewA</h3>
<ul>
{{#each viewA}}
<li>
{{keyA}}/{{msg}}
</li>
{{/each}}
</ul>
<h3>ViewB</h3>
<ul>
{{#each viewB}}
<li>
{{keyB}}/{{msg}}
</li>
{{/each}}
</ul>
</template>
var MultiView = new Meteor.Collection('multiView');
var findKeyA = function() {
return MultiView.find({'keyA':{$exists:true}});
};
var findKeyB = function() {
return MultiView.find({'keyB':{$exists:true}});
};
if (Meteor.isClient) {
Meteor.subscribe("multiA");
Meteor.subscribe("multiB");
Template.hello.viewA = function () {
return findKeyA();
};
Template.hello.viewB = function () {
return findKeyB();
};
}
if (Meteor.isServer) {
Meteor.startup(function () {
MultiView.remove({});
MultiView.insert({keyA:'one', msg:'yaho'});
MultiView.insert({keyA:'two', msg:'yahoo'});
MultiView.insert({keyB:'hana', msg:'yoho'});
MultiView.insert({keyB:'dul', msg:'yohoo'});
MultiView.insert({keyC:'notMe', msg:'yohoo'});
});
Meteor.publish('multiA', function () {
return findKeyA();
});
Meteor.publish('multiB', function () {
return findKeyB();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment