themeteorchef / bert

A client side, multi-style alerts system for Meteor.
185 stars 27 forks source link

Way to call from the server side? #31

Closed jetlej closed 7 years ago

jetlej commented 8 years ago

How can I create a Bert Alert from within a server-side Meteor method?

I have a system that awards badges in the background while users actions occur on the front-end, and I want to tell them as soon as they have been rewarded a new badge.

themeteorchef commented 8 years ago

Hm, this sounds like it would best be done using collection.observe. So, whenever the change you're waiting for happens, on the client, you can call Bert.

For example:

Badges.find({ userId }).observe({
  added() {
    Bert.alert('You got a new badge!', 'success');
  }
});
aniskhan001 commented 7 years ago

It always shows an alert message whether a new record is added or not. If a new record added, the alert is shown twice.

themeteorchef commented 7 years ago

@aniskhan001 this will fire whenever the client receives data (e.g., the result of a publication being subscribed to technically fires an "added" event because docs are being added from the server).

Gobliins commented 7 years ago

Hello, i face the same Problem:

import { Bert } from 'meteor/themeteorchef:bert'; 
...
Meteor.methods({
  'foo'() {
    Bert.alert(...);
  },
});

results in TypeError: Cannot call method 'alert' of undefined

themeteorchef commented 7 years ago

@Gobliins Bert is only intended for use on the client, so you'll need to use it in the callback to your Method call like:

    Meteor.call('myMethod', {}, (error, response) => {
      if (error) {
        Bert.alert(error.reason, 'danger');
      } else {
        Bert.alert('Success message', 'success');
      }
    });