From 4e7377bbbd6373422125cf5214fe3e810936bdf7 Mon Sep 17 00:00:00 2001 From: Jeremy Jackson Date: Fri, 24 Jun 2011 20:43:17 -0600 Subject: [PATCH] Updated modal and dialog to load from the preloaded views if possible --- .../javascripts/mercury/dialog.js.coffee | 25 +++--- .../javascripts/mercury/modal.js.coffee | 19 ++-- .../javascripts/mercury/dialog_spec.js.coffee | 90 +++++++++++-------- spec/javascripts/mercury/modal_spec.js.coffee | 0 4 files changed, 79 insertions(+), 55 deletions(-) create mode 100644 spec/javascripts/mercury/modal_spec.js.coffee diff --git a/app/assets/javascripts/mercury/dialog.js.coffee b/app/assets/javascripts/mercury/dialog.js.coffee index 3462d35..66d0228 100644 --- a/app/assets/javascripts/mercury/dialog.js.coffee +++ b/app/assets/javascripts/mercury/dialog.js.coffee @@ -57,16 +57,21 @@ class @Mercury.Dialog load: (callback) -> return unless @url - jQuery.ajax @url, { - success: (data) => - @loadContent(data) - Mercury.dialogHandlers[@name].call(@) if Mercury.dialogHandlers[@name] - callback() if callback - error: => - @hide() - @button.removeClass('pressed') if @button - alert("Mercury was unable to load #{@url} for the #{@name} dialog.") - } + if Mercury.preloadedViews[@url] + @loadContent(Mercury.preloadedViews[@url]) + Mercury.dialogHandlers[@name].call(@) if Mercury.dialogHandlers[@name] + callback() if callback + else + jQuery.ajax @url, { + success: (data) => + @loadContent(data) + Mercury.dialogHandlers[@name].call(@) if Mercury.dialogHandlers[@name] + callback() if callback + error: => + @hide() + @button.removeClass('pressed') if @button + alert("Mercury was unable to load #{@url} for the #{@name} dialog.") + } loadContent: (data) -> diff --git a/app/assets/javascripts/mercury/modal.js.coffee b/app/assets/javascripts/mercury/modal.js.coffee index 4db9b8e..3a017e0 100644 --- a/app/assets/javascripts/mercury/modal.js.coffee +++ b/app/assets/javascripts/mercury/modal.js.coffee @@ -125,14 +125,17 @@ jQuery.extend Mercury.modal, { load: -> @element.addClass('loading') @setTitle() - jQuery.ajax @url, { - type: @options.loadType || 'get' - data: @options.loadData - success: (data) => @loadContent(data) - error: => - @hide() - alert("Mercury was unable to load #{@url} for the modal.") - } + if Mercury.preloadedViews[@url] + setTimeout((=> @loadContent(Mercury.preloadedViews[@url])), 10) + else + jQuery.ajax @url, { + type: @options.loadType || 'get' + data: @options.loadData + success: (data) => @loadContent(data) + error: => + @hide() + alert("Mercury was unable to load #{@url} for the modal.") + } loadContent: (data, options) -> diff --git a/spec/javascripts/mercury/dialog_spec.js.coffee b/spec/javascripts/mercury/dialog_spec.js.coffee index 8df4ab8..a30f918 100644 --- a/spec/javascripts/mercury/dialog_spec.js.coffee +++ b/spec/javascripts/mercury/dialog_spec.js.coffee @@ -184,60 +184,76 @@ describe "Mercury.Dialog", -> Mercury.dialogHandlers.foo = -> @dialog = new Mercury.Dialog('/evergreen/responses/blank.html', 'foo', {appendTo: '#test', for: $('#button')}) - it "makes an ajax request", -> - spy = spyOn($, 'ajax').andCallFake(=>) - @dialog.load() - expect(spy.callCount).toEqual(1) - it "does nothing if there's no url", -> spy = spyOn($, 'ajax').andCallFake(=>) @dialog.url = false @dialog.load() expect(spy.callCount).toEqual(0) - describe "on success", -> + describe "on a preloaded view", -> beforeEach -> - @loadContentSpy = spyOn(Mercury.Dialog.prototype, 'loadContent').andCallFake(=>) - @ajaxSpy = spyOn($, 'ajax').andCallFake (url, options) => - options.success('return value') if (options.success) + Mercury.preloadedViews = {'/evergreen/responses/blank.html': 'this is the preloaded content'} - it "calls loadContent with data", -> - @dialog.load() - expect(@loadContentSpy.callCount).toEqual(1) - expect(@loadContentSpy.argsForCall[0]).toEqual(['return value']) + afterEach -> + Mercury.preloadedViews = {} - it "calls a dialog handler if there's one", -> - spy = spyOn(Mercury.dialogHandlers, 'foo').andCallFake(=>) + it "calls loadContent with the content in the preloaded view", -> + spy = spyOn(Mercury.Dialog.prototype, 'loadContent').andCallFake(=>) @dialog.load() expect(spy.callCount).toEqual(1) + expect(spy.argsForCall[0]).toEqual(['this is the preloaded content']) - it "calls a callback if one was provided", -> - spy = spyOn(@, 'spyFunction').andCallFake(=>) - @dialog.load(@spyFunction) - expect(spy.callCount).toEqual(1) - - describe "on failure", -> - - beforeEach -> - @alertSpy = spyOn(window, 'alert').andCallFake(=>) - @ajaxSpy = spyOn($, 'ajax').andCallFake (url, options) => - options.error() if (options.error) + describe "when not a preloaded view", -> - it "hides", -> - spy = spyOn(Mercury.Dialog.prototype, 'hide').andCallFake(=>) + it "makes an ajax request", -> + spy = spyOn($, 'ajax').andCallFake(=>) @dialog.load() expect(spy.callCount).toEqual(1) - it "removes the pressed state for it's button", -> - $('#button').addClass('pressed') - @dialog.load() - expect($('#button').hasClass('pressed')).toEqual(false) - - it "alerts the user", -> - @dialog.load() - expect(@alertSpy.callCount).toEqual(1) - expect(@alertSpy.argsForCall[0]).toEqual(['Mercury was unable to load /evergreen/responses/blank.html for the foo dialog.']) + describe "on success", -> + + beforeEach -> + @loadContentSpy = spyOn(Mercury.Dialog.prototype, 'loadContent').andCallFake(=>) + @ajaxSpy = spyOn($, 'ajax').andCallFake (url, options) => + options.success('return value') if (options.success) + + it "calls loadContent with data", -> + @dialog.load() + expect(@loadContentSpy.callCount).toEqual(1) + expect(@loadContentSpy.argsForCall[0]).toEqual(['return value']) + + it "calls a dialog handler if there's one", -> + spy = spyOn(Mercury.dialogHandlers, 'foo').andCallFake(=>) + @dialog.load() + expect(spy.callCount).toEqual(1) + + it "calls a callback if one was provided", -> + spy = spyOn(@, 'spyFunction').andCallFake(=>) + @dialog.load(@spyFunction) + expect(spy.callCount).toEqual(1) + + describe "on failure", -> + + beforeEach -> + @alertSpy = spyOn(window, 'alert').andCallFake(=>) + @ajaxSpy = spyOn($, 'ajax').andCallFake (url, options) => + options.error() if (options.error) + + it "hides", -> + spy = spyOn(Mercury.Dialog.prototype, 'hide').andCallFake(=>) + @dialog.load() + expect(spy.callCount).toEqual(1) + + it "removes the pressed state for it's button", -> + $('#button').addClass('pressed') + @dialog.load() + expect($('#button').hasClass('pressed')).toEqual(false) + + it "alerts the user", -> + @dialog.load() + expect(@alertSpy.callCount).toEqual(1) + expect(@alertSpy.argsForCall[0]).toEqual(['Mercury was unable to load /evergreen/responses/blank.html for the foo dialog.']) describe "#loadContent", -> diff --git a/spec/javascripts/mercury/modal_spec.js.coffee b/spec/javascripts/mercury/modal_spec.js.coffee new file mode 100644 index 0000000..e69de29