Skip to content

Commit

Permalink
Updated modal and dialog to load from the preloaded views if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jejacks0n committed Jun 25, 2011
1 parent 3e3729f commit 4e7377b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 55 deletions.
25 changes: 15 additions & 10 deletions app/assets/javascripts/mercury/dialog.js.coffee
Expand Up @@ -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) ->
Expand Down
19 changes: 11 additions & 8 deletions app/assets/javascripts/mercury/modal.js.coffee
Expand Up @@ -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) ->
Expand Down
90 changes: 53 additions & 37 deletions spec/javascripts/mercury/dialog_spec.js.coffee
Expand Up @@ -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", ->
Expand Down
Empty file.

0 comments on commit 4e7377b

Please sign in to comment.