diff --git a/examples/other/template-demo/client/template-demo.js b/examples/other/template-demo/client/template-demo.js index 0f2f9fe4bd..877f54706d 100644 --- a/examples/other/template-demo/client/template-demo.js +++ b/examples/other/template-demo/client/template-demo.js @@ -137,32 +137,6 @@ Template.timer.destroyed = function () { /////////////////////////////////////////////////////////////////////////////// -// Run f(). Record its dependencies. Rerun it whenever the -// dependencies change. -// -// Returns an object with a stop() method. Call stop() to stop the -// rerunning. -// -// XXX this should go into Meteor core as Meteor.autorun -var autorun = function (f) { - var ctx; - var slain = false; - var rerun = function () { - if (slain) - return; - ctx = new Meteor.deps.Context; - ctx.run(f); - ctx.on_invalidate(rerun); - }; - rerun(); - return { - stop: function () { - slain = true; - ctx.invalidate(); - } - }; -}; - Template.d3Demo.left = function () { return { group: "left" }; }; @@ -230,7 +204,7 @@ Template.circles.rendered = function () { if (! self.handle) { d3.select(self.node).append("rect"); - self.handle = autorun(function () { + self.handle = Meteor.autorun(function () { var circle = d3.select(self.node).selectAll("circle") .data(Circles.find({group: data.group}).fetch(), function (d) { return d._id; }); diff --git a/packages/deps/deps-utils.js b/packages/deps/deps-utils.js index 5f71923e90..3e050f2b02 100644 --- a/packages/deps/deps-utils.js +++ b/packages/deps/deps-utils.js @@ -1,5 +1,7 @@ (function () { + ////////// Meteor.deps.ContextSet + // Constructor for an empty ContextSet. // // A ContextSet is used to hold a set of Meteor.deps.Contexts that @@ -51,4 +53,32 @@ }; Meteor.deps.ContextSet = ContextSet; + + ////////// Meteor.autorun + + // Run f(). Record its dependencies. Rerun it whenever the + // dependencies change. + // + // Returns an object with a stop() method. Call stop() to stop the + // rerunning. Also passes this object as an argument to f. + Meteor.autorun = function (f) { + var ctx; + var slain = false; + var handle = { + stop: function () { + slain = true; + ctx.invalidate(); + } + }; + var rerun = function () { + if (slain) + return; + ctx = new Meteor.deps.Context; + ctx.run(function () { f.call(this, handle); }); + ctx.on_invalidate(rerun); + }; + rerun(); + return handle; + }; + })(); \ No newline at end of file