Re: Re: WindowBus.js.js

From disqus.com, 2 Months ago, written in JavaScript, viewed 3 times. This paste is a reply to Re: Re:Events.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/bda4ba8b Embed
Download Paste or View Raw
  1. define('core/WindowBus',[
  2.     'jquery',
  3.     'underscore',
  4.     'backbone',
  5.     'modernizr',
  6. ], function (
  7.     $,
  8.     _,
  9.     Backbone,
  10.     Modernizr
  11. ) {
  12.     'use strict';
  13.  
  14.     /**
  15.      * WindowBus: A cross-frame / tab message passing bus.
  16.      *
  17.      * WindowBus passes messages using localStorage,
  18.      * which means messages are secure, though they persist
  19.      * after the browser session closes.
  20.      *
  21.      * Messages may only be passed between the same domains.
  22.      */
  23.     var WindowBus = Backbone.Model.extend({
  24.         initialize: function () {
  25.             if (Modernizr.localstorage)
  26.                 $(window).on('storage', _.bind(this.onStorageEvent, this));
  27.         },
  28.  
  29.         broadcast: function (name, data) {
  30.             if (!Modernizr.localstorage)
  31.                 return;
  32.  
  33.             var message = JSON.stringify({
  34.                 name: name,
  35.                 data: data,
  36.  
  37.                 // We need to append a cache buster to the storage key so that
  38.                 // `storage` events are correctly triggered in other frames =/
  39.                 time: new Date().getTime(),
  40.             });
  41.  
  42.             try {
  43.                 window.localStorage.setItem(this.constructor.STORAGE_KEY, message);
  44.             } catch (error) {
  45.                 // TODO: log error w/ Raven.js
  46.             }
  47.  
  48.         },
  49.  
  50.         onStorageEvent: function (evt) {
  51.             var key = evt.originalEvent.key;
  52.             var message = evt.originalEvent.newValue;
  53.  
  54.             if (!(message && key === this.constructor.STORAGE_KEY))
  55.                 return;
  56.  
  57.             try {
  58.                 message = JSON.parse(message);
  59.                 this.trigger(message.name, message.data);
  60.             } catch (error) {
  61.                 // TODO: log error w/ Raven.js
  62.             }
  63.         },
  64.     }, {
  65.         STORAGE_KEY: 'disqus.bus',
  66.     });
  67.  
  68.     return WindowBus;
  69. });
  70. // https://c.disquscdn.com/next/next-core/core/WindowBus.js
  71.  

Replies to Re: Re: WindowBus.js.js rss

Title Name Language When
Re:WindowBus.js disqus.com javascript 2 Months ago.

Reply to "Re: Re: WindowBus.js.js"

Here you can reply to the paste above