流星/铁路由器反应性无限循环



所以我有一个路由,在渲染时我更新文档,也被用来渲染页面上的数据。

但是,当我通过onRun修改数据时,我得到一个无限循环。:(在数据函数中这样做并不能解决问题,因为该方法也是反应性的,因为我可以使用的所有方法(onAfterAction, onBeforeAction等)。onBeforeAction将是完美的,如果我可以使用它…

ViewRoomController = RouteController.extend({
    data: function() {
        return Rooms.findOne(this.params._id);
    },
    onRun: function() {
        var room = Rooms.findOne(this.params._id),
            that = this,
            messages;
        if(room !== undefined) {
            messages = room.messages;
            //Do stuff with messages...
            //This will go into an infinite loop.
            //setTimeout is here just so loop doesn't lock up process.
            setTimeout(function() {
                Rooms.update(
                    that.params._id, {
                        $set: {
                            messages: messages
                        }
                    }
                );
            }, 1);
        }
    }
});

我打赌我错过了一些愚蠢的东西,但我已经在这个问题上困了一个多小时了…:)

我发现正确的方法是将ID传递给模板,然后让模板本身响应,而不是整个路由。

你知道的越多。:)

最新更新