Skip to content

Commit

Permalink
build v0.2.3-alpha.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Miller committed Nov 4, 2014
1 parent c357a16 commit f831050
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
35 changes: 26 additions & 9 deletions dist/unsavedChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,20 @@ angular.module('unsavedChanges', ['resettable'])
require: '^form',
link: function(scope, formElement, attrs, formCtrl) {

// @todo refactor, temp fix for issue #22
// where user might use form on element inside a form
// we shouldnt need isolate scope on this, but it causes the tests to fail
// traverse up parent elements to find the form.
// we need a form element since we bind to form events: submit, reset
var count = 0;
while(formElement[0].tagName !== 'FORM' && count < 3) {
count++;
formElement = formElement.parent();
}
if(count >= 3) {
throw('unsavedWarningForm must be inside a form element');
}

// register this form
unsavedWarningSharedService.init(formCtrl);

Expand All @@ -260,14 +274,12 @@ angular.module('unsavedChanges', ['resettable'])
// do things like reset validation, present messages, etc.
formElement.bind('reset', function(event) {
event.preventDefault();
// because we bind to `resetResettables` also when
// dismissing alerts, we need to apply() in this
// instance to ensure the model view updates.
// @note for ngActiveResoruce, where the models
// themselves do validation, we can't rely on just
// setting the form to valid - we need to set each
// model value back to valid.
scope.$apply($rootScope.$broadcast('resetResettables'));

// trigger resettables within this form or element
var resettables = angular.element(formElement[0].querySelector('[resettable]'));
if(resettables.length) {
scope.$apply(resettables.triggerHandler('resetResettables'));
}

// sets for back to valid and pristine states
formCtrl.$setPristine();
Expand Down Expand Up @@ -295,14 +307,17 @@ angular.module('unsavedChanges', ['resettable'])
* to original value.
* --------------------------------------------
*
* @note we don't create a seperate scope so the model value
* is still available onChange within the controller scope.
* This fixes https://github.com/facultymatt/angular-unsavedChanges/issues/19
*
*/
angular.module('resettable', [])

.directive('resettable', ['$parse', '$compile', '$rootScope',
function($parse, $compile, $rootScope) {

return {
scope: true,
restrict: 'A',
link: function postLink(scope, elem, attr, ngModelCtrl) {

Expand All @@ -320,6 +335,8 @@ angular.module('resettable', [])
setter(scope, originalValue);
};

elem.on('resetResettables', resetFn);

// @note this doesn't work if called using
// $rootScope.on() and $rootScope.$emit() pattern
var removeListenerFn = scope.$on('resetResettables', resetFn);
Expand Down
2 changes: 1 addition & 1 deletion dist/unsavedChanges.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f831050

Please sign in to comment.