Dev Note: This module is still in development. However it's used in many of my production projects so it can be considered stable and battle tested.
This directive will alert users when they navigate away from a page where a form has unsaved changes. It will be triggered in all situations where form data would be lost:
In addition this module:
$locationChangeStart and $stateChangeStart The directive binds to locationChangeStart and window.onbeforeunload. When these events happen all registered froms are checked if they are dirty. The module defers to the forms $dirty property as a single source of truth. If dirty, the user is alerted. Disregarding changes resets the form and sets pristine.
$ bower install angular-unsavedChanges --save.<script src="https://github.com/facultymatt/angular-unsavedChanges/raw/v0.1.1/bower_components/angular-unsavedChanges/dist/unsavedChanges.js"></script>.angular.module('app', ['unsavedChanges', 'anotherDirective'])unsaved-changes-warningThe module provides two directives for use.
Add to forms you want to register with directive. The module will only listen when forms are registered.
<form name="testForm" unsaved-warning-form>
</form>
Add to button or link that will disregard changes, preventing the messaging when user tries to navigate. Note that button type should be reset to work with lazy-model directive (outlined below).
<form name="testForm" unsaved-warning-form>
<input name="test" type="text" ng-model="test"/>
<button type="submit"></button>
<button type="reset" unsaved-warning-clear></button>
</form>
A number of options can be configured. The module uses the Object.defineProperty pattern. This avoids the need for custom getters and setters and allows us to treat configuration as pure JS objects.
Defaults to true. Will use translate service if available. It's safe to leave this set to true, even when not using the translate service, because the module still checks that the service exists.
unsavedWarningsConfigProvider.useTranslateService = true;
Defaults to false. Uses the services internal logging method for debugging.
unsavedWarningsConfigProvider.logEnabled = true;
Defaults to ['$locationChangeStart' ,'$stateChangeStart'] which supports ui router by default.
unsavedWarningsConfigProvider.routeEvent = '$stateChangeStart';
Set custom message displayed when user navigates. If using translate this will be the key to translate.
unsavedWarningsConfigProvider.navigateMessage = "Custom Navigate Message";
Set custom message displayed when user refreshes the page. If using translate this will be the key to translate.
unsavedWarningsConfigProvider.reloadMessage = "Custom Reload Message";
This module includes a customized version of Lazy Model. Lazy model ensures that model changes are only persisted when user submits valid form. It also resets model values to their original value when form is reset.
To use this simply add lazy-model to your inputs instead of ng-model. Submitting the form will update your model, while clicking "clear changes" will reset the model values to their original state.
<input name="test" type="text" lazy-model="test"/>
*** Known issue: sometimes the form is removed from expected scope. Ie: in your controller $scope.formName no longer works. You might need to access $scope.$$childTail.formName. This will be fixed in furture versions.
To try the demo run npm install && bower install && grunt connect. The browser should open http://127.0.0.1:9001/demo.
Note you need to manually change the paths in index.html and karam-unit.conf to point to the dist version for final testing. Make sure to run $ grunt first.
End 2 End Testing Because of the alert / event driven nature of this module it made the most sense to rely on e2e tests. (also its hard to interact with alerts via unit tests).
To run the e2e tests do the following:
webdriver-manager start (or use other selenium methods as per Protractor documentation.)$ grunt test:e2eUnit Tests
$ grunt test:unit OR $ grunt testRun $ grunt to lint and minify the code. Also strips console logs.
$ claude mcp add angular-unsavedChanges \
-- python -m otcore.mcp_server <graph>