#TernopilJS
Лекція 8. Publish/Subscribe
Лектор - Михайло
Код - github.com/ternopiljs
Презентації - slides.com/ternopiljs
Новини - vk.com/ternopiljs
Observers
Publish/subscribe
Allow/Deny
Observe
Posts.find().observe({
added: function (doc) {
console.log('Post\'s added', doc);
},
changed: function (doc) {
console.log('Post\'s changed', doc);
},
removed: function (doc) {
console.log('Post\'s removed', doc);
}
});
Observe
Posts.find().observeChanges({
added: function (id, fields) {
console.log('Post\'s added', id, fields);
},
changed: function (id, doc) {
console.log('Post\'s changed', id, fields);
},
removed: function (id, doc) {
console.log('Post\'s removed', id, fields);
}
});
Observe
var handle = Posts.find().observeChanges({
added: function (id, fields) {
console.log('Post\'s added', id, fields);
},
changed: function (id, doc) {
console.log('Post\'s changed', id, fields);
},
removed: function (id, doc) {
console.log('Post\'s removed', id, fields);
}
});
handle.stop(); // stops observing
> meteor remove autopublish
Publish
Meteor.publish('publish-name', function(argument1, argument2) {
return Collection.find({field1: argument1, field2: argument2});
});
Meteor.subscribe('publish-name', argument1, argument2);
Subscribe
Publish. Low-level interface
Meteor.publish('publish-name', function() {
var self = this;
var handle = SomeCollection.find().observe({
added: function (doc) {
self.added('collection-name', doc._id, doc);
},
changed: function (doc) {
self.changed('collection-name', doc._id, doc);
},
removed: function (oldDoc) {
self.removed('collection-name', oldDoc._id);
}
});
self.ready();
self.onStop(function () {
handle.stop();
});
});
Meteor.subscribe('publish-name', argument1, argument2, {
onReady: function () {},
onStop: function () {}
});
Subscribe handle
var handle = Meteor.subscribe('publish-name', argument1, argument2, {
onReady: function () {},
onStop: function () {}
});
handle.stop() // зупиняє підписку
handle.ready() // реактивне джерело
// True, якщо метод ready був викликаний в публікації
Reactive Subscribe
Tracker.autorun(function () {
Meteor.subscribe("images", Session.get("limit"));
});
> meteor remove insecure
Allow
Posts.allow({
insert: function (userId, doc) {
// the user must be logged in, and the document must be owned by the user
return (userId && doc.owner === userId);
},
update: function (userId, doc, fields, modifier) {
// can only change your own documents
return doc.owner === userId;
},
remove: function (userId, doc) {
// can only remove your own documents
return doc.owner === userId;
},
fetch: ['owner']
});
Deny
Posts.deny({
insert: function (userId, doc) {
return !!userId;
},
update: function (userId, doc, fields, modifier) {
return doc.owner !== userId;
},
remove: function (userId, doc) {
return false;
},
fetch: ['owner']
});
У наступній лекції:
Д/З
Переробити Д/З з використанням Layout