Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
fix(macModal): Fixed modal callbacks firing on page loads
Browse files Browse the repository at this point in the history
Fixes #162
  • Loading branch information
adrianlee44 committed Mar 20, 2014
1 parent daa10bc commit 240a7af
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
10 changes: 4 additions & 6 deletions src/directives/modal.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ angular.module("Mac").directive("macModal", [
if angular.element($event.target).hasClass("modal-overlay")
$scope.$apply -> modal.hide()

for callback in ["beforeShow", "afterShow", "beforeHide", "afterHide"]
key = "macModal#{util.capitalize(callback)}"
if attrs[key]? and attrs[key]
opts[callback] = $parse(attrs[key]) $scope
for callback in ["beforeShow", "afterShow", "beforeHide", "afterHide", "open"]
key = "macModal#{util.capitalize(callback)}"
opts[callback] = $parse(attrs[key]) or angular.noop

registerModal = (id) ->
if id? and id
regId = id
opts.open = $parse(attrs.macModalOpen)($scope)
regId = id
modal.register id, element, opts

if attrs.id
Expand Down
48 changes: 26 additions & 22 deletions src/services/modal.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -75,76 +75,77 @@ angular.module("Mac").service("modal", [
else if @registered[id]?
modalObject = @registered[id]
options = modalObject.options
showOptions = {}

# Extend options from trigger with modal options
angular.extend options, triggerOptions

options.beforeShow?()
angular.extend showOptions, options, triggerOptions

showModal = (element) =>
showOptions.beforeShow element.scope()

$animate.removeClass element, "hide", =>
$animate.addClass element, "visible", =>

# Update opened modal object
@opened = {id, element, options}
@opened = {id, element, options: showOptions}
@resize @opened
@bindingEvents()

options.open?()
options.afterShow?()
showOptions.open element.scope()
showOptions.afterShow element.scope()

$rootScope.$broadcast "modalWasShown", id
@clearWaiting()

# if modal is created thru module method "modal"
if options.moduleMethod?
if showOptions.moduleMethod?
renderModal = (template) =>
# Scope allows either a scope or an object:
# - Scope - Use the scope to compile modal
# - Object - Creates a new "isolate" scope and extend the isolate
# scope with data being passed in
#
# Use the scope passed in
if isScope(options.scope)
viewScope = options.scope
if isScope(showOptions.scope)
viewScope = showOptions.scope

# Create an isolated scope and extend scope with value pass in
else
viewScope = $rootScope.$new(true)
if angular.isObject options.scope
angular.extend viewScope, options.scope
if angular.isObject showOptions.scope
angular.extend viewScope, showOptions.scope

angular.extend options.attributes, {id}
element = angular.element(@modalTemplate).attr options.attributes
angular.extend showOptions.attributes, {id}
element = angular.element(@modalTemplate).attr showOptions.attributes
wrapper = angular.element(
element[0].getElementsByClassName("modal-content-wrapper")
)
wrapper.html template

if options.overlayClose
if showOptions.overlayClose
element.bind "click", ($event) =>
if angular.element($event.target).hasClass("modal-overlay")
viewScope.$apply => @hide()

if options.controller
if showOptions.controller
# Modal controller has the following locals:
# - $scope - Current scope associated with the element
# - $element - Current modal element
# - macModalOptions - Modal options
#
# macModalOptions is added to give user more information in the
# controller when compiling modal
$controller options.controller,
$controller showOptions.controller,
$scope: viewScope
$element: element
macModalOptions: options
macModalOptions: showOptions

$animate.enter element, angular.element(document.body)
$compile(element) viewScope

showModal element

if (path = options.templateUrl)
if (path = showOptions.templateUrl)
template = $templateCache.get path
if template
renderModal template
Expand All @@ -154,7 +155,7 @@ angular.module("Mac").service("modal", [
renderModal resp.data
, ->
throw Error("Failed to load template: #{path}")
else if (template = options.template)
else if (template = showOptions.template)
renderModal template

# modal created using modal directive
Expand Down Expand Up @@ -204,7 +205,7 @@ angular.module("Mac").service("modal", [
return unless @opened?

{id, options, element} = @opened
options.beforeHide?()
options.beforeHide element.scope()

$animate.removeClass element, "visible", =>
@bindingEvents "unbind"
Expand All @@ -220,7 +221,7 @@ angular.module("Mac").service("modal", [
else
$animate.addClass element, "hide"

options.afterHide?()
options.afterHide element.scope()

$rootScope.$broadcast "modalWasHidden", id

Expand Down Expand Up @@ -253,7 +254,10 @@ angular.module("Mac").service("modal", [
if @registered[id]?
throw new Error "Modal #{id} already registered"

@registered[id] = {id, element, options}
modalOpts = {}
angular.extend modalOpts, modalViews.defaults, options

@registered[id] = {id, element, options: modalOpts}

if @waiting? and @waiting.id is id
@show id, @waiting.options
Expand Down

0 comments on commit 240a7af

Please sign in to comment.