Skip to content

Instantly share code, notes, and snippets.

@yeputons
Created February 2, 2014 14:08
Show Gist options
  • Save yeputons/8768973 to your computer and use it in GitHub Desktop.
Save yeputons/8768973 to your computer and use it in GitHub Desktop.
Demonstration of publish-with-relations bug
<head>
<title>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() {
return Meteor.publishWithRelations({
handle: this,
collection: A,
filter: {},
mappings: [
{
key: 'bId',
collection: B
}
]
});
});
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});
}
}
{
"packages": {
"publish-with-relations": {}
}
}
{
"meteor": {},
"dependencies": {
"basePackages": {
"publish-with-relations": {}
},
"packages": {
"publish-with-relations": {
"git": "https://github.com/erundook/meteor-publish-with-relations.git",
"tag": "v0.1.5",
"commit": "98213e9f056b139597c99fb9ec2884ae6be76d91"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment