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

Commit

Permalink
refactor(macModal): Cleaned up how scope is included with mac-modal t…
Browse files Browse the repository at this point in the history
…rigger and updated how scope is created on compile
  • Loading branch information
adrianlee44 committed Feb 4, 2014
1 parent 2992e35 commit e7bf343
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
12 changes: 4 additions & 8 deletions src/directives/modal.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,14 @@ directive("macModal", [
($parse, modal) ->
restrict: "A"
link: ($scope, element, attrs) ->
if attrs.macModal
element.bind "click", ->
modalScope = false
if attrs.macModalScope? and attrs.macModalScope
modalScope = $parse(attrs.macModalScope)($scope)
modalScope = $scope unless modalScope? and modalScope.$new?
return unless attrs.macModal

element.bind "click", ->
$scope.$apply ->
data = $parse(attrs.macModalData)($scope) or {}
modal.show attrs.macModal,
data: data
scope: modalScope
modalScope.$apply()
scope: $scope
return
]).

Expand Down
37 changes: 27 additions & 10 deletions src/services/modal.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,20 @@ angular.module("Mac").service("modal", [
# if modal is created thru module method "modal"
if options.moduleMethod?
renderModal = (template) =>
viewScope =
if options.scope then options.scope.$new() else $rootScope.$new(true)
viewScope.close = ($event, force = false) =>
if force or (options.overlayClose and
angular.element($event.target).hasClass("modal-overlay"))
@hide()

if options.controller
$controller options.controller,
$scope: viewScope
# 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

# 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

angular.extend options.attributes, {id}
element = angular.element(@modalTemplate).attr options.attributes
Expand All @@ -119,6 +123,19 @@ angular.module("Mac").service("modal", [
if angular.element($event.target).hasClass("modal-overlay")
viewScope.$apply => @hide()

if options.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,
$scope: viewScope
$element: element
macModalOptions: options

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

Expand Down

0 comments on commit e7bf343

Please sign in to comment.