Skip to content

Instantly share code, notes, and snippets.

@yeputons
Last active August 29, 2015 13:55
Show Gist options
  • Save yeputons/8780479 to your computer and use it in GitHub Desktop.
Save yeputons/8780479 to your computer and use it in GitHub Desktop.
Reactive-publish-test
<head>
<title>reactive-publish-test</title>
</head>
<body>
{{> hello}}
</body>
<template name="hello">
<p>Elements of A:</p>
<ul>
{{#each a}}
<li>{{_id}}. B = {{bId}}. <input class="newb" value="{{bId}}" id="bId-{{_id}}"/> <a href="#" class="updb">Update</a></li>
{{/each}}
</ul>
<p>Elements of B: (<a href="#" class="subscr">Subscribe</a>, <a href="#" class="unsubscr">Unsubscribe</a>)</p>
<ul>
{{#each b}}
<li>{{_id}}</li>
{{/each}}
</ul>
</template>
A = new Meteor.Collection('a');
B = new Meteor.Collection('B');
if (Meteor.isClient) {
Meteor.subscribe('As');
Template.hello.helpers({
a: function() { return A.find(); },
b: function() { return B.find(); }
});
var bSubscr;
Template.hello.events({
'click .updb': function(e, instance) {
A.update(this._id, {$set: {bId: instance.find("#bId-" + this._id).value}});
},
'click .subscr': function() {
if (bSubscr) return;
bSubscr = Meteor.subscribe('Bs');
},
'click .unsubscr': function() {
if (bSubscr) {
bSubscr.stop();
bSubscr = undefined;
}
}
});
}
if (Meteor.isServer) {
// Meteor.publish('As', function() {
Meteor.reactivePublish('As', function() {
console.log('tryit');
var as = A.find({}, {reactive: true}).fetch();
console.log('as=', as);
return [
A.find({}),
B.find({_id: {$in: _.pluck(as, 'bId')}})
];
});
Meteor.publish('Bs', function() {
return B.find();
});
if (!A.find().count() && !B.find().count()) {
var id = B.insert({});
B.insert({});
B.insert({});
A.insert({bId: id});
A.insert({bId: id});
}
var allowAll = {
insert: function() { return true; },
update: function() { return true; },
remove: function() { return true; }
};
A.allow(allowAll);
B.allow(allowAll);
}
{
"packages": {
"reactive-publish": {}
}
}
{
"meteor": {},
"dependencies": {
"basePackages": {
"reactive-publish": {}
},
"packages": {
"reactive-publish": {
"git": "https://github.com/Diggsey/meteor-reactive-publish.git",
"tag": "v0.1.4",
"commit": "a1bed70a44cd4c9680ce817503282cbfaa8a7cb1"
},
"server-deps": {
"git": "https://github.com/Diggsey/meteor-server-deps.git",
"tag": "v0.1.2",
"commit": "2db5d15fad9240723549cbbc5260a74b6b7e1c61"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment