Monday, 26 August 2013

Meteor.call inside an Collection.observeChanges

Meteor.call inside an Collection.observeChanges

I have a collection (called items) and am trying to watch for a change on
one of the items in the collection via Collection.observeChanges. Once a
change is encountered I want to call a method on the server side with the
ID of my object.
I can't figure out for the life of me why this code isn't working.
Here is the minimal example
Client
Template.test.items = function() {
return Items.find();
}
Template.test.events({
'click a.create.btn' : function () {
Items.insert({count: 0});
}
});
Template.item.events({
'click a.change.btn' : function() {
Items.update(this._id, {$inc: {count: 1}})
}
})
Meteor.startup(function() {
Items.find().observeChanges({
changed: function(id, fields) {
Meteor.call("testMethod", "CHANGED!");
}
});
});
Server
Meteor.startup(function () {
Meteor.publish('items', function() { return Items.find() });
});
Meteor.methods({
testMethod: function(message){
console.log(message);
}
})
Both
this.Items = new Meteor.Collection("items");
html
<body>
{{> test }}
</body>
<template name="test">
<a class="create btn">Create Item</a>
<ul>



</ul>
</template>
<template name="item">
<li>
<h2>Item</h2>
<span>Count is: {{ count }}</span>
<a id="{{_id}}" class="change btn">Increment</a>
</li>
</template>

No comments:

Post a Comment