Meteor应用程序上的警报/通知不会出现 [英] Alerts/Notifications on Meteor application do not appear

查看:152
本文介绍了Meteor应用程序上的警报/通知不会出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的警报集合。它显示未读消息的数量。消息被提交并显示,并且控制台或服务器上没有其他错误。

I created a new "Alerts" collection. Its to show number of unread messages. Messages get submitted and appear, and theres no other error on console or server.

第二个问题是,当我点击特定房间时,它应该将所有新消息标记为已读。不知怎的,这个数字还有。错误显示排队任务中的​​异常:.added @ http:// localhost:3000 / app / lib / collections / messages.js

2nd issue is, when i click on the specific room, it is supposed to mark all new messages as "read". Somehow the number stays. Error shows Exception in queued task: .added@http://localhost:3000/app/lib/collections/messages.js

文件结构:


  • roomList.js - 显示所有房间的列表,显示编号未读消息

  • roomDetail.js - 当单击列表中的特定房间时,将消息标记为

    read,未读数字消失。

  • alerts.js(提醒集合)

  • messages.js(消息集合)

  • rooms.js(Rooms collection)

  • roomList.js - shows a list of all rooms, shows number of unread messages
  • roomDetail.js - when click specific room in list, will mark message as
    "read", unread number dissapears.
  • alerts.js (Alerts collection)
  • messages.js (Messages collection)
  • rooms.js (Rooms collection)

刊物和子js

Meteor.publish('alerts', function() {
    return Alerts.find({ userId: this.userId, read: false });
});
Meteor.subscribe('alerts')

提醒集合js

Alerts = new Mongo.Collection('alerts');
Alerts.allow({
   update: ownsDocument,

   //if removed, shows error: 
   // insert failed: Access denied. No allow validators set on restricted collection for method 'insert'.
   insert: function(){  
      return true;
   }
});

createMessageAlert = function(message) {
  if ( message.user !== Meteor.userId() ){     
      Alerts.insert({
         userId        : message.user,
         roomId        : Router.current().params._id, //params id of current room
         messageId     : message._id,
         read          : false
      });
   }
};

roomDetail.js

  Messages.insert({          
     roomId    : Router.current().params._id,
     msg       : message,
     user      : Meteor.user()._id
  });
  template.find('input').value = '';
  createMessageAlert(message); 

roomsList.js

Template.list.helpers({
   alerts: function (){
      return Alerts.find({ userId: Meteor.userId(), read: false });
   },
   alertCount: function(){
      return Alerts.find({ userId: Meteor.userId(), read: false }).count();
   }
});

Template.allRooms.events({
   'click a': function() {     //click the a href to go into roomDetail
      Alerts.update(this._id, {$set: {read: true}});
   }
});


推荐答案

终极解决方案:

消息集合中添加新消息时,应从触发器调用 createMessageAlert

You should call the createMessageAlert from a trigger when a new Message is added in Messages collection.

先决条件:


  1. 创建一个用于Messages集合的触发器(MSG_OBSERVER),无论何时向集合添加任何内容,都会随添加的文档对象一起调用 createMessageAlert 方法,因此您可以在方法内部进行操作并执行所需的操作。

  2. 当您更新提醒集合时。该集合应以这样的方式发布(名为null),它应该是被动的,并且应该可以从不同浏览器实例访问同一帐户的所有实例中获得。

  1. create a trigger(MSG_OBSERVER) for Messages collection, where whenever anything is added to the collection, a createMessageAlert method is invoked provided with the added document object, so you can play inside the method and do desired operations.
  2. When you are updating Alerts collection. The collection should be published in such a way(named as "null") that it should be reactive and should be available from all the instances accessing the same account from different browser instances.

实施

只需在collections.js中添加以下代码

Just add below code in your collections.js

Meteor.method(
'createMessageAlert': function(id, fields) {
      if ( fields.user !== Meteor.userId() ){ 
          Alerts.insert({
             userId        : fields.user,
             roomId        : Router.current().params._id, //params id of current room
             messageId     : id,
             read          : false
          });
       }
    }
);

var MSG_OBSERVER = Messages.find();

MSG_OBSERVER.observe({
  added: function(id, fields){
        Meteor.call('createMessageAlert', id, fields);
  }
});

Meteor.publish(null ,function() { // null name means send to all clients
        //use Messages.insert() to generate a fake message and add data in below params
        Meteor.call('createMessageAlert', id, fields);
        return Alerts.find();
});

解释


  1. 如果您再次阅读先决条件,您将理解代码。确保您在客户端订阅了所需的集合。此代码使每个集合都涉及并触发非常响应和响应。

  2. 随着消息的添加将添加到警报中。

  3. 发布null只会将数据发布到所有创建UI行为的客户端更强大和异步。(我在显示实时图表时使用此功能,您甚至不必刷新UI并反映您的数据。)

  4. 发布null时,您可以创建一个假的Message OBJ并将其添加到 createMessageAlert 函数中。您必须执行此操作,因为您必须在服务器重新启动时启动发布。明智地选择消息Obj,这样就不会影响工作流程。

  1. If you again read the pre-requisites, you will understand the code. Ensure you are subscribed with desired collection on client side. This code makes every collection involved and triggers very reactive and responsive.
  2. Whatever you will add as messages will be added to Alerts as well.
  3. Publishing "null" will simply publish data to all clients making UI behavior more robust and asynchronous.(I am using this feature in displaying real-time graphs, you don't even have to refresh UI and your data gets reflected.)
  4. While publishing "null", you can create a fake Message OBJ and add it to call createMessageAlert function. You have to do this because you have to initiate publish on server restarts. choose Message Obj wisely so that it won't impact the work flow.

这篇关于Meteor应用程序上的警报/通知不会出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆