Last active
December 20, 2015 15:19
-
-
Save mikowals/6153533 to your computer and use it in GitHub Desktop.
Meteor reactive publish with collection join
answering Stack Overflow question ( http://stackoverflow.com/questions/17902480/how-do-i-make-a-collection-reactive-based-on-the-contents-of-another)
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>publishTest</title> | |
| </head> | |
| <body> | |
| {{> hello}} | |
| </body> | |
| <template name="hello"> | |
| <h1>Hello World!</h1> | |
| {{greeting}} | |
| <br> | |
| Item name: Category Name | |
| {{#each items}} | |
| <br> | |
| {{name}} : {{category}} | |
| {{/each}} | |
| <br> | |
| <br> | |
| <label>Item:</label> | |
| <input id="itemName" type="text" placeholder="Name"/> | |
| <input id="itemCat" type="text" placeholder="Category"/> | |
| <input id="addItem" type="button" value="Add" /> | |
| <br> | |
| <label>Item:</label> | |
| <input id="rmItemName" type="text" placeholder="Name"/> | |
| <input id="removeItem" type="button" value="Remove" /> | |
| <br> | |
| <label>Category:</label> | |
| <input id="inactiveCat" type="text" placeholder="Category"/> | |
| <input id="inactive" type="button" value="Inactive" /> | |
| <br> | |
| <label>Category:</label> | |
| <input id="activeCat" type="text" placeholder="Category"/> | |
| <input id="active" type="button" value="Active" /> | |
| </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
| var Items = new Meteor.Collection("items"); | |
| var Categories = new Meteor.Collection("categories"); | |
| if (Meteor.isClient) { | |
| Meteor.subscribe ( "items"); | |
| Template.hello.greeting = function () { | |
| return "Welcome to publishTest."; | |
| }; | |
| Template.hello.events({ | |
| 'click #inactive' : function () { | |
| Meteor.call( "inactive", $('#inactiveCat').val() ); | |
| }, | |
| 'click #active' : function(){ | |
| Meteor.call( "active", $('#activeCat').val() ); | |
| console.log( $('#activeCat').val()); | |
| }, | |
| 'click #addItem' : function () { | |
| Meteor.call( "addItem", $('#itemName').val(), $('#itemCat').val() ); | |
| }, | |
| 'click #removeItem' : function(){ | |
| Meteor.call( "removeItem", $('#rmItemName').val() ); | |
| } | |
| }); | |
| Template.hello.items = function(){ | |
| return Items.find(); | |
| }; | |
| } | |
| if (Meteor.isServer) { | |
| Meteor.methods({ | |
| inactive : function ( categoryName ){ | |
| var cat = Categories.findOne ( {category: categoryName} ); | |
| if ( cat ) Categories.update( cat._id, {$set: {active: false}} ); | |
| }, | |
| active : function ( categoryName ){ | |
| var cat = Categories.findOne ( {category: categoryName} ); | |
| if ( cat ) Categories.update( cat._id, {$set: {active: true}} ); | |
| console.log( cat ); | |
| }, | |
| addItem : function ( name, categoryName ){ | |
| var cat = Categories.findOne ( {category: categoryName} ); | |
| if ( cat ) Items.insert( { name: name, categoryId: cat._id, category: categoryName}); | |
| }, | |
| removeItem : function ( name ){ | |
| var item = Items.findOne ( {name: name} ); | |
| if ( item ) Items.remove( item._id ); | |
| } | |
| }); | |
| Meteor.publish( "items", function(){ | |
| var self = this; | |
| var categories = []; | |
| var handle = Categories.find({ active: true}).observeChanges({ | |
| added: function(id){ | |
| categories.push( id ); | |
| console.log ( "category added"); | |
| }, | |
| removed: function( id ){ | |
| categories.splice ( categories.indexOf( id ), 1); | |
| console.log ( "category removed"); | |
| } | |
| }); | |
| self.onStop( function() { | |
| handle.stop(); | |
| }); | |
| return Items.find( {categoryId: {$in: categories}} ); | |
| }); | |
| Meteor.startup(function () { | |
| // code to run on server at startup | |
| Items.remove({}); | |
| Categories.remove({}); | |
| Categories.insert( {category: "1", active: true }); | |
| Categories.insert( {category: "2", active: true }); | |
| Categories.insert( {category: "3", active: false }); | |
| Categories.find().forEach( function ( category ) { | |
| Items.insert( { name: "item 1", categoryId: category._id, category: category.category}); | |
| Items.insert( { name: "item 2", categoryId: category._id, category: category.category }); | |
| Items.insert( { name: "item 3", categoryId: category._id, category: category.category }); | |
| }); | |
| }); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment