diff --git a/src/directives/modal.coffee b/src/directives/modal.coffee index ae247e0..15267fb 100644 --- a/src/directives/modal.coffee +++ b/src/directives/modal.coffee @@ -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 ]). diff --git a/src/services/modal.coffee b/src/services/modal.coffee index 60ccdce..5eba4b6 100644 --- a/src/services/modal.coffee +++ b/src/services/modal.coffee @@ -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 @@ -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