Skip to content

Instantly share code, notes, and snippets.

@raix
Last active October 10, 2015 16:09
Show Gist options
  • Save raix/c840b4578a2afc8172a7 to your computer and use it in GitHub Desktop.
Save raix/c840b4578a2afc8172a7 to your computer and use it in GitHub Desktop.
Meteor Copenhagen hackathon - publish/restpoint data
Meteor.methods({
'addFoo': function(name) {
check(name, String);
foo.insert({ owner: this.userId, name });
}
});
foo = new Mongo.Collection('foo');
friends = new Mongo.Collection('friends');
Meteor.subscribe('allMyFoo');
Meteor.subscribe('myFriends');
Template.hello.helpers({
// foo: () => foo.find()
foo: function() {
return foo.find();
},
friends: function() {
return friends.find();
}
});
<head>
<title>test-app</title>
</head>
<body>
<h1>Welcome to Meteor!</h1>
{{> loginButtons}}
<br>
<br>
{{> hello}}
</body>
<template name="hello">
Friends:<br>
{{#each friends}}
{{name}}<br>
{{else}}
No friends huh?
{{/each}}
<br>
<br>
<br>
{{#each foo}}
{{_id}} -
{{#if name}}
{{name}}
{{else}}
Dude add a name man...
{{/if}}
<br>
{{else}}
Dude add some stuff man...
{{/each}}
</template>
# autopublish # Publish all data to the clients (for prototyping)
# insecure # Allow all DB writes from clients (for prototyping)
random
accounts-password
accounts-ui
check
ejson
http
foo = new Mongo.Collection('foo');
restpointCollection = new Mongo.Collection('restpointCollection');
Meteor.publish('allMyFoo', function() {
return foo.find({ owner: this.userId });
});
foo.allow({
'insert': function(userId, doc) {
return userId && doc.owner === userId;
},
'update': function(userId, doc) {
return doc.owner === userId;
},
'remove': function(userId, doc) {
return doc.owner === userId;
}
});
Meteor.publish('myFriends', function() {
if (this.userId) {
// This will contain the client version of the restpoint documents
var clientDocuments = {
// 'xxxxx': new Date()
};
var ready = false;
var interval = Meteor.setInterval(() => {
var result = HTTP.get(Meteor.absoluteUrl('friends'));
if (result.statusCode === 200) {
// var time = EJSON.parse(result.content);
var allDocuments = EJSON.fromJSONValue(result.data);
var allDocumentsObject = {};
_.each(allDocuments, (doc) => {
allDocumentsObject[doc._id] = doc;
if (clientDocuments[doc._id]) {
if (clientDocuments[doc._id] !== doc.updateAt) {
console.log(`Updated the doc for userId: ${this.userId} docId: ${doc._id}`);
clientDocuments[doc._id] = doc.updatedAt || true;
this.changed('friends', doc._id, doc);
}
} else {
console.log(`Insert the doc for userId: ${this.userId} docId: ${doc._id}`);
clientDocuments[doc._id] = doc.updatedAt || true; // updateAt
this.added('friends', doc._id, doc);
}
});
_.each(clientDocuments, (doc, id) => {
if (!allDocumentsObject[id]) {
console.log(`Remove the doc for userId: ${this.userId} docId: ${id}`);
delete clientDocuments[id];
this.removed('friends', id, {});
}
});
// Let the client know we are ready...
if (!ready) {
ready = true;
this.ready();
}
} else {
console.log(`Updated the time for userId: ${this.userId} Failed...`);
}
}, 1000);
this.onStop(() => {
Meteor.clearInterval(interval);
console.log(`The userId: ${this.userId} has unsubscribed :(`);
});
} else {
console.log('Dude this user needs to log in...');
return [];
}
});
// Rest point
WebApp.connectHandlers.use("/friends", function(req, res, next) {
var result = restpointCollection.find().fetch();
res.writeHead(200, {
'Content-Type': 'application/json'
});
// console.log('RESTPOINT provided the documents (' + result.length + ')');
res.end(EJSON.stringify(result));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment