Question

I'm using Pnotify to send a notification, but the notification is shown only in very specific places. I want to append them to the header class in my page, but I'm not sure how to do it.

JS:

function showNotify(data){
    var notice = $.pnotify({
        type: 'success',
        delay: 300000,
        addclass: (isDevice)? 'body-device stack-topleft':'stack-topleft',
        mouse_reset: false
    }).click(function(e){
        notice.pnotify_remove();
    });
}

The HTML that I want to append my pnotify to is:

<div class="header">
    <div class="col-md-12" data-bind="with: activeRoute"></div>
</div>
Was it helpful?

Solution

From http://sciactive.com/pnotify/#demos-simple

function show_stack_context(type) {
    if (typeof stack_context === "undefined") stack_context = {
        "dir1": "down",
        "dir2": "left",
        "context": $("#stack-context")
    };
    var opts = {
        title: "Over Here",
        text: "Check me out. I'm in a different stack.",
        stack: stack_context
    };
    switch (type) {
    case 'error':
        opts.title = "Oh No";
        opts.text = "Watch out for that water tower!";
        opts.type = "error";
        break;
    case 'info':
        opts.title = "Breaking News";
        opts.text = "Have you met Ted?";
        opts.type = "info";
        break;
    case 'success':
        opts.title = "Good News Everyone";
        opts.text = "I've invented a device that bites shiny metal asses.";
        opts.type = "success";
        break;
    }
    $.pnotify(opts);
}

so you would

function showNotify(data){
    if (typeof stack_context === "undefined") stack_context = {
        "dir1": "down",
        "dir2": "left",
        "context": $(".header")
    };
    var opts = {
        title: "Over Here",
        text: "Notification",
        type: 'success',
        delay: 300000,
        addclass: (isDevice)? 'body-device stack-topleft':'stack-topleft',
        mouse_reset: false
        stack: stack_context
    };
    var notice = $.pnotify(opts).click(function(e){
        notice.pnotify_remove();
    });
}

OTHER TIPS

Update to address how to append pnotify to header class

After i have reread your question i assume you might be looking for something like in this updated sample Below the data-attribute provides the title and text for the pnotify message.

<div class="header" 
          data-title="notify 1" 
          data-text="text 1">Header with autowiring of pnotify</div>

<div class="header" 
          data-title="better notify 2" 
          data-text="other text 2">Header 2</div>

To wire pnotify up with the header class you can add this code:

 function getNotifiyMessage(that){      
      var mytitle = $(that).attr("data-title" );
      var mytext = $(that).attr("data-text");          
      return { title: mytitle, text: mytext};
    };

    $(document).ready(function() {

          $(".header").click(
            function(){
              var notifyMessage = getNotifiyMessage(this);
              $.pnotify(notifyMessage);
            }
          );
    });    

Older first answer

In case you mean this pnotify based of the sample

<button class="btn btn-default source" 
      onclick="$.pnotify({ title: 'Regular Notice'
            , text: 'i am a note from pnotify'});"
  >Regular Notice</button>

You can create a pnotify by adding an onclick attribute with the function to activate pnotify. See this sample Below the data-attribute provides the title and text for the pnotify message.

<div class="header" 
  onclick="$.pnotify({ title: 'Regular Notice', text: 'i am a note from pnotify'});">
  <div class="col-md-12" 
       data-bind="with: activeRoute">go click me to see pnotify</div>
</div>

Note that above the event bubbles up from the inner div to the outer div and then the pnotify fires.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top