From 6b5f44f981e8a3ddcb7f24a6f6c0670b6a04cf42 Mon Sep 17 00:00:00 2001 From: Ryan Shaw Date: Thu, 29 Mar 2012 14:37:04 -0600 Subject: [PATCH] trigger userContent change without killing ie8 Change-Id: I876006c0cabdb45ac2695b0061913f4f38d5a5a9 Reviewed-on: https://gerrit.instructure.com/9699 Reviewed-by: Zach Wily Tested-by: Hudson --- app/coffeescripts/discussions/EntryView.coffee | 7 ++++--- public/javascripts/instructure.js | 11 ++++++++++- .../javascripts/vendor/jquery.ba-tinypubsub.js | 16 +++++++++++----- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/app/coffeescripts/discussions/EntryView.coffee b/app/coffeescripts/discussions/EntryView.coffee index 74ec940411d..fa91d8345bc 100644 --- a/app/coffeescripts/discussions/EntryView.coffee +++ b/app/coffeescripts/discussions/EntryView.coffee @@ -10,12 +10,13 @@ define [ 'compiled/discussions/EntryEditor' 'compiled/discussions/MarkAsReadWatcher' 'str/htmlEscape' + 'vendor/jquery.ba-tinypubsub' 'compiled/jquery.kylemenu' # entry_with_replies partials 'jst/_avatar' 'jst/discussions/_reply_form' -], (require, I18n, Backbone, EntryCollection, entryContentPartial, deletedEntriesTemplate, entryWithRepliesTemplate, Reply, EntryEditor, MarkAsReadWatcher, htmlEscape) -> +], (require, I18n, Backbone, EntryCollection, entryContentPartial, deletedEntriesTemplate, entryWithRepliesTemplate, Reply, EntryEditor, MarkAsReadWatcher, htmlEscape, {publish}) -> # save memory noop = -> @@ -84,11 +85,11 @@ define [ @$el.html entryWithRepliesTemplate @model.toJSON() @$el.attr 'data-id', @model.get 'id' @$el.attr 'id', @model.cid - super # enhance the media_comments in the message - $(document).trigger('user_content_change') + publish('userContent/change') + super openMenu: (event, $el) -> @createMenu($el) unless @$menu diff --git a/public/javascripts/instructure.js b/public/javascripts/instructure.js index 9435fa9bb88..13fa4fb5176 100644 --- a/public/javascripts/instructure.js +++ b/public/javascripts/instructure.js @@ -486,7 +486,16 @@ define([ alert(I18n.t('alerts.file_previews_disabled', 'File previews have been disabled for this Canvas site')); }); } - $(document).bind('user_content_change', $.throttle(50, enhanceUserContent)); + + // publishing the 'userContent/change' will run enhanceUserContent at most once every 50ms + var enhanceUserContentTimeout; + $.subscribe('userContent/change', function(){ + clearTimeout(enhanceUserContentTimeout); + enhanceUserContentTimeout = setTimeout(enhanceUserContent, 50); + }); + + + $(document).bind('user_content_change', enhanceUserContent); setInterval(enhanceUserContent, 15000); setTimeout(enhanceUserContent, 1000); diff --git a/public/javascripts/vendor/jquery.ba-tinypubsub.js b/public/javascripts/vendor/jquery.ba-tinypubsub.js index f59aca6cec9..c97246e2bda 100644 --- a/public/javascripts/vendor/jquery.ba-tinypubsub.js +++ b/public/javascripts/vendor/jquery.ba-tinypubsub.js @@ -11,17 +11,17 @@ define(['jquery'], function($){ // Create a "dummy" jQuery object on which to bind, unbind and trigger event // handlers. Note that $({}) works in jQuery 1.4.3+. - var o = $({}); + var o = $({}), subscribe, unsubscribe, publish; // Subscribe to a topic. Works just like bind, except the passed handler // is wrapped in a function so that the event object can be stripped out. // Even though the event object might be useful, it is unnecessary and // will only complicate things in the future should the user decide to move // to a non-$.event-based pub/sub implementation. - $.subscribe = function( topic, fn ) { + $.subscribe = subscribe = function( topic, fn ) { if ($.isPlainObject(topic)) { return $.each(topic, function(topic, fn) { - $.subscribe(topic, fn); + subscribe(topic, fn); }); } // Call fn, stripping out the 1st argument (the event object). @@ -38,13 +38,19 @@ define(['jquery'], function($){ }; // Unsubscribe from a topic. Works exactly like unbind. - $.unsubscribe = function() { + $.unsubscribe = unsubscribe = function() { o.unbind.apply( o, arguments ); }; // Publish a topic. Works exactly like trigger. - $.publish = function() { + $.publish = publish = function() { o.trigger.apply( o, arguments ); }; + return { + subscribe: subscribe, + unsubscribe: unsubscribe, + publish: publish + }; + });