Last active
August 29, 2015 13:57
-
-
Save mikowals/9676242 to your computer and use it in GitHub Desktop.
Subscription leak
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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