In Meteor, how to set a reactive dependency on a subpart of a template data context?
meteor
Solution
You could recreate the `isolateValue` behaviour in a way which doesn't cause `Template.instance()` to get set to `null` sometimes.
$ meteor add reactive-var
Template.fullDoc.rendered = function () {
var docIdVar = new ReactiveVar();
this.autorun(function () {
docIdVar.set(Template.currentData().selectedDoc._id);
});
this.autorun(function () {
var docId = docIdVar.get();
// ...
});
}
This makes use of the fact that setting a `ReactiveVar` to the same value it already has will not trigger an invalidation. (By default this only works for primitives; for objects you'll need to pass a custom `equalsFunc` when you construct the `ReactiveVar`. If `_id` is a string, you're fine. If it's `ObjectID` you probably aren't.)
Problem
Consider the following code : ``` Template.fullDoc.rendered = function() { // Get triggered whenever the selected document id changes this.autorun(function() { var docId = isolateValue(function() { return Template.currentData().selectedDoc._id; }); ... }); } ``` This code doesn't work. Inside `isolateValue()`, `Template.currentData()` sometimes triggers an exception: `Exception from Tracker recompute function: Error: There is no current view` (this corresponds to the fact that `Template.instance()` returns `null`). So how do you set a reactive dependency on a subpart of a template data context?