locked
Messaging in the HTML Client RRS feed

  • Question

  • Hi all,

    I have incorporated SignalR and toastr into my app; all works great. Basically toastr pops up a message via SignalR whenever certain entities are created/updated.

    However the code that responds to SignalR is exposed at the screen level, i.e I have to recreate the code for each screen. Since the message content itself (at least in this scenario) is NOT screen-specific, I'm wondering if any kind soul could point me a best-case strategy for globally receiving the message, irrespective of whichever actual 'page' is currently on screen.

    An example of some screen code is below, obviously it's this that I want to 'globalise'. I have a feeling I may be missing something obvious but that's typical when you are in 'deep-dev' mode on a project!

    /// <reference path="../GeneratedArtifacts/viewModel.js" />
    /// <reference path="../Scripts/jquery.signalR-2.0.0.js" />
    /// <reference path="../Scripts/toastr.js" />
    
    myapp.BrowseContacts.created = function (screen) {
    
        // At screen creation
        $(function ()
        {
            // Register screen with SignalR for messages
            var cnn = $.connection.signalRHub;
            cnn.client.broadcastMessage = function (message)
            {
                // Send messages from SignalR to toastr
    
                toastr.options = {
                    "closeButton": true,
                    "debug": false,
                    "positionClass": "toast-bottom-full-width",
                    "onclick": null,
                    "showDuration": "300",
                    "hideDuration": "1000",
                    "timeOut": "5000",
                    "extendedTimeOut": "1000",
                    "showEasing": "swing",
                    "hideEasing": "linear",
                    "showMethod": "fadeIn",
                    "hideMethod": "fadeOut"
                };
                
                toastr.info(message);
            };
    
            // Start SignalR hub
            $.connection.hub.start()
            .done(function ()
            {
                // Nothing required; operation OK
            })
            .fail(function ()
            {
                // SignalR broadcasting failure
                alert("Could not connect to broadcast messaging system - no other application functionality is affected");
            });
        });
    };


    Ian Mac

    Monday, November 25, 2013 2:16 PM

Answers

  • Hi Ian,

    I would try and put it directly inside the default.htm file. You can put the entire contents of the created function above inside the $(document).ready() function like so:

     $(document).ready(function () {
         msls._run()
             .then(null, function failure(error) {
                alert(error);
             });
    
         ... put your code here...
    
    });

    I cannot see why this will not work globally, unless there is some JQM magic that prevents it.

    Let us know how this works for you.

    EDIT: Btw, I don't think you need the outer $(function() {} part.


    Regards, Xander. My Blog

    • Edited by novascape Monday, November 25, 2013 8:16 PM comment
    • Marked as answer by Ian Mac Tuesday, November 26, 2013 11:05 AM
    Monday, November 25, 2013 8:13 PM

All replies

  • Hi Ian,

    I would try and put it directly inside the default.htm file. You can put the entire contents of the created function above inside the $(document).ready() function like so:

     $(document).ready(function () {
         msls._run()
             .then(null, function failure(error) {
                alert(error);
             });
    
         ... put your code here...
    
    });

    I cannot see why this will not work globally, unless there is some JQM magic that prevents it.

    Let us know how this works for you.

    EDIT: Btw, I don't think you need the outer $(function() {} part.


    Regards, Xander. My Blog

    • Edited by novascape Monday, November 25, 2013 8:16 PM comment
    • Marked as answer by Ian Mac Tuesday, November 26, 2013 11:05 AM
    Monday, November 25, 2013 8:13 PM
  • Hi Xander,

    You know, I'm an idiot, thinking too deep as I said. Default.htm, so no code-behind, so no go. Except of course that I can put code in directly, which I've already done for other things. Duh, how does that sort of thing happen? Go figure!

    Many thanks!


    Ian Mac

    Tuesday, November 26, 2013 11:05 AM