HubSpot / messenger

Growl-style alerts and messages for your app. #hubspot-open-source
http://github.hubspot.com/messenger/
MIT License
4.03k stars 408 forks source link

How to use with $get or Ajax request? #92

Closed biapar closed 9 years ago

biapar commented 9 years ago

How to use with $get or Ajax request? Thx

geekjuice commented 9 years ago

If you take a look at the documentation, we actually provide many examples on how to use messenger in various ways. In fact, the first example shows how to use it with an ajax request and below are a few other examples as well. Hopefully you find what you need in there.

biapar commented 9 years ago

Yes, I read the docs before. Do I change if all $.ajax calls into the code or not?

Il giorno 14/mag/2015, alle ore 15:46, Nicholas Hwang notifications@github.com ha scritto:

If you take a look at the documentation, we actually provide many examples on how to use messenger in various ways. In fact, the first example shows how to use it with an ajax request and below are a few other examples as well. Hopefully you find what you need in there.

— Reply to this email directly or view it on GitHub.

geekjuice commented 9 years ago

Do you mind providing an example of what you are trying to accomplish? A jsFiddle would be even better.

In the meantime, if I were to guess what you were asking for, I would probably say the answer is no, you do not need to replace all $.ajax, just the ones you want messenger to alert for on success/error of the response. For example:

let messages = {
  successMessage: "Roger that!",
  errorMessage: "Abort mission!"
};

Messenger().run(messages, {
  url: '/ready',
  success(response, type, xhr) {
   successHandler(response);
  },
  error(xhr, type, statusText) {
    errorHandler(xhr);
  }
});

If your response was a success, messenger will display the successMessage and in the success callback, you can do whatever you need to with the response (as if from $.ajax). Likewise for the when the response fails. Basically the above is equivalent to:

$.ajax({
  url: '/ready',
  type: 'GET',
  dataType: 'json',
  success(response, type, xhr) {
    Messenger().post(messages.successMessage);
    successHandler(response);
  },
  error(xhr, type, statusText) {
    Messenger().post(messages.errorMessage);
    errorHandler(xhr);
  }
});