【问题标题】:How to use meteor Event Hooks如何使用流星事件挂钩
【发布时间】:2015-03-25 22:47:13
【问题描述】:

我想显示当前在线并且关注特殊页面的用户。我喜欢在此页面的某处显示用户列表和状态(在线/离线焦点/无焦点)

我找到了这个包:benjaminrh/event-hooks

设置很清楚,这在文档中。但我不明白如何使用 Hooks。例如:

Hooks.onLoseFocus = function ([server: userId]) { ... } 

(anywhere) - 提供一个回调以在窗口失去焦点时运行。

所以该函数需要一个 userId。

考虑到上述布局,我将在某个地方添加一个额外的模板和一个 each 循环,例如:

  {{#each userstatus}}
     {{>users_stats}
  {{/each}}

当钩子获取单个用户 ID 时,我将如何创建用户状态?

【问题讨论】:

    标签: meteor event-hooking


    【解决方案1】:

    好的,这是一个完全有效的解决方案,它跟踪当前查看特定路线路径的用户并通过帮助程序显示此用户列表。这在您使用iron:router 的假设下有效:

    服务器

    Hooks.onCloseSession = function() {
        Meteor.call('clearUserFocus', function(error, result) {
            if(error)
                console.log(error);
        });
    }
    
    Meteor.methods({
        'setUserFocus': function(_routePath) {
            Meteor.users.update({_id: Meteor.userId()}, {$set: {focus: _routePath}});
        },
        'clearUserFocus': function() {
            var userId = Meteor.userId();
            Meteor.users.update({_id: userId}, {$unset: {focus: ''}});
        }
    });
    

    onCloseSession 似乎只能在服务器中可靠地工作,正如您所期望的那样。

    客户

    Meteor.startup(function() {
        // Start hooks, for user focus tracking
        Hooks.init({
            updateFocus: 500
        });
    });
    
    Hooks.onGainFocus = function() {
        // Router.current().route.getName(); // route name
        var routePath = Iron.Location.get().pathname;  // route path
        Meteor.call('setUserFocus', routePath, function(error, result) {
            if(error)
                console.log(error);
        });
    }
    
    Hooks.onLoseFocus = function() {
        Meteor.call('clearUserFocus', function(error, result) {
            if(error)
                console.log(error);
        });
    }
    

    客户端全局路由器

    Router.onBeforeAction(function() {
        // Record user focus
        var routePath = Iron.Location.get().pathname;
        Meteor.call('setUserFocus', routePath, function(error, result) {
            if(error)
                console.log(error);
        });
    
        this.next();
    });
    

    客户端模板助手

    Template.yourTemplateName.helpers({
        'focusedUsers': function() {
                var routePath = Iron.Location.get().pathname;
                return Meteor.users.find({focus: routePath});
         }
    });
    

    客户模板

    <ul>
        {{#each focusedUsers}}
            <li>{{_id}}</li>
        {{/each}}
    </ul>
    

    这对我来说效果很好,但我需要彻底测试它,因为我发现了至少一个警告。运行多个窗口时,它似乎会与焦点混淆。

    另请注意,您需要所有路由都可以访问users 发布(无论您的发布是什么),以便助手可以访问集合并获取focus。你可以这样做:

    // Global router settings
    Router.configure({
        layoutTemplate: 'defaultLayout',
        loadingTemplate: 'loading',
        waitOn: function() {
            return Meteor.subscribe('users');
        }
    });
    

    根据要求:请注意,这包括默认布局和加载模板的可选示例。加载模板会替换 layoutTemplate {{> yield}} 的内容,直到 waitOn(全局和特定路由)订阅准备就绪。

    我还收到了一个浏览器控制台错误,据我所知,这似乎并没有妨碍任何事情,但我不喜欢它在那里:'Error invoking Method 'eventsOnHooksInit': Method not found [ 404]'

    【讨论】:

    • 在文档中,我已经在我的问题中复制了它,也是Hooks.onLoseFocus = function ([server: userId]) { ... } (任何地方) - 提供一个回调以在窗口失去焦点时运行。 --- 该代码将在客户端站点上转到哪里? --- 如何创建当前关注一个特殊页面的用户列表?
    • @MBushveld 我将把这个功能集成到我正在开发的应用程序中,我会告诉你我是如何实现的。你在用 Iron:router 吗?
    • @MBushveld 这是一个完整的解决方案,请注意注意事项
    • 谢谢!我只是试一试。关于客户端部分:我需要将其仅放在客户端站点上还是需要与特定站点建立关系? - 我假设第一个。关于路由器:我假设我在路由器中建立了与特定站点的关系,所以我需要将“onBeforeAction”放入特定路由?例如 this.route('profile'), { .... 再次感谢!
    • 我认为关于路由器的部分我可以弄清楚。我假设 onBeforAction 是一个全局配置。我在一个示例中找到了Router.onBeforeAction('loading'); 如何将其集成到上述代码中?加载在示例中提供了一个我喜欢保留的等待圈。
    猜你喜欢
    • 2015-06-05
    • 2019-08-29
    • 2011-12-25
    • 1970-01-01
    • 2011-05-16
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多