Skip to content

Instantly share code, notes, and snippets.

@mikowals
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save mikowals/9676242 to your computer and use it in GitHub Desktop.

Select an option

Save mikowals/9676242 to your computer and use it in GitHub Desktop.
Subscription leak
<head>
<title>Leak</title>
</head>
<body>
{{> main}}
{{> serverFacts}}
</body>
<template name="main">
{{#if page }}
{{> template1}}
{{else}}
{{> template2}}
{{/if}}
</template>
<template name="template1">
<h1>template1</h1>
<ul>
{{#each data}}
<li> {{num}}</li>
{{/each}}
</ul>
</template>
<template name="template2">
<h1>template2</h1>
<ul>
{{#each data}}
<li> {{num}}</li>
{{/each}}
</ul>
</template>
Data = new Meteor.Collection('data');
if (Meteor.isClient) {
Template.main.page = function(){
return Session.equals("page", "template1");
};
Template.main.events({
'click': function(){
var newPage = Session.equals("page", "template1") ? "template2" : "template1";
Session.set( "page", newPage );
}
});
//XXX this rendered function causes extra subscriptions on the server. Uncomment and use Deps.autorun() below.
Template.main.rendered = function(){
Meteor.subscribe( 'data', Session.get("page"));
};
Deps.autorun( function(){
//Meteor.subscribe( 'data', Session.get("page"));
});
Template.template1.data = function(){
return Data.find();
};
Template.template2.data = function(){
return Data.find();
};
}
if (Meteor.isServer) {
Meteor.startup(function () {
if ( Data.find().count() < 10 ){
_.times( 20, function( num ){
Data.insert( {num: num } );
});
}
});
Meteor.publish( 'data', function( page ){
var criteria;
if ( page === "template1" ) {
criteria = { num: {$lt: 10}};
} else{
criteria = { num: {$gte: 10}};
}
return Data.find( criteria );
});
Facts.setUserIdFilter(function () { return true; });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment