Skip to content

Instantly share code, notes, and snippets.

@yeputons
Created February 16, 2014 15:11
Show Gist options
  • Save yeputons/9035816 to your computer and use it in GitHub Desktop.
Save yeputons/9035816 to your computer and use it in GitHub Desktop.
<head>
<title>publish-test</title>
</head>
<body>
{{> hello}}
</body>
<template name="hello">
<p>Elements of A: (<a href="#" class="subscr">Subscribe</a>, <a href="#" class="unsubscr">Unsubscribe</a>)</p>
<ul>
{{#each a}}
<li>{{_id}}. XXX={{xxx}}. B = {{bId}}. <input class="newb" value="{{bId}}" id="bId-{{_id}}"/> <a href="#" class="updb">Update</a></li>
{{/each}}
</ul>
<p>Elements of B:</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('Bs');
Template.hello.helpers({
a: function() { return A.find(); },
b: function() { return B.find(); }
});
var aSubscr;
Template.hello.events({
'click .updb': function(e, instance) {
A.update(this._id, {$set: {bId: instance.find("#bId-" + this._id).value}});
},
'click .subscr': function() {
if (aSubscr) return;
aSubscr = Meteor.subscribe('As');
},
'click .unsubscr': function() {
if (aSubscr) {
aSubscr.stop();
aSubscr = undefined;
}
}
});
}
if (Meteor.isServer) {
Meteor.publish('Bs', function() {
return Meteor.publishWithRelations({
handle: this,
collection: B,
filter: {},
mappings: [
{
reverse: true,
key: 'bId',
collection: A,
}
]
});
});
Meteor.publish('As', function() {
return A.find();
});
if (!A.find().count() && !B.find().count()) {
var id = B.insert({});
A.insert({bId: id});
A.insert({bId: id});
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