Giter Club home page Giter Club logo

angular-auto-save-form's Introduction

angular-auto-save-form

npm version dependencies Status devDependencies Status downloads

Description

Angular auto save form changed inputs.
The directive will call the callback function with a parameter object containing only the inputs that are $dirty.

Install with Bower

  bower install angular-auto-save-form --save

Install with npm

  npm install angular-auto-save-form --save

Then add a <script> to your index.html:

  <link rel="stylesheet" href="bower_components/angular-auto-save-form/dist/auto-save-form.css">
  <script src="bower_components/angular/angular.js"></script>
  <script src="bower_components/lodash/lodash.js"></script>
  <script src="bower_components/angular-auto-save-form/dist/auto-save-form.js"></script>

Include 'angular-auto-save-form' as a dependency of your module like this:

  var module = angular.module("example", ["angular-auto-save-form"]);

Usage

Default usage:

Directive requires that form and input elements to have [name] attribute

  <ng-form name="myForm" auto-save-form="callback(controls)"> 
    <input ng-model="user.name" name="name"/>
    <input ng-model="user.email" name="email"/>
  </ng-form>

Which expects a scope setup like the following:

  $scope.user = { name: "Jon Doe", email: "[email protected]" };
  
  //changing input user.name the callback function will be called with parameter object
  $scope.callback = function(controls){ // controls = {'name': 'Jon Doe'}
      return $http.post('saveDataUrl', controls);
  };

For radio inputs or if you want to group inputs on the same property use the [auto-save-form-property] attribute
on one of the inputs and prefix the input name with a group name

  <ng-form name="myForm" auto-save-form="callback(controls)"> 
    <input type="radio" ng-model="user.gender" name="inputGroupName.gender1" 
      auto-save-form-property="inputGroupName.gender" value="male"/>Male
    <input type="radio" ng-model="user.gender" name="inputGroupName.gender2" value="female"/>Female
  </ng-form>

The object will look like this:

  //{'gender': 'male'}

Optional attributes:

If you want to change locally debounce timer

  auto-save-form-debounce="number" default:500

If you want to change the debounce at input level use [ng-model-options] directive

Loading spinner in top right corner of the form enabled by default if callback promise returns a promise.

  auto-save-form-spinner="boolean"  default:true
  auto-save-form-spinner-position="string"  default:'top right'

Possible combinations: 'top right', 'top left', 'bottom left', 'bottom right'.
It is possible to add your own class without your desired position.
Example:

[auto-save-form] .spinner.my-class {
    top: 50%;
    left: 50%;
  }
  auto-save-form-spinner-position="my-class"
The directive supports nested objects like:
 user = {
  name: 'Jon Doe',
  country: {
    name: 'French',
    city: 'Paris'
  }
 }
  <input ng-model="user.country.name" name="country.name"/>
//callback object
 {
  country: {
    name: 'French'
  }
 }

Alternatively, disable auto save usage:

Warning: Mode false works only with form tag see this issue
  <form name="myForm" auto-save-form="callback(controls, $event)" auto-save-form-mode="boolean"> 
    <input ng-model="username" name="user"/>
  </form>

Which expects a scope setup like the following:

  $scope.username = "Jon Doe";
  
  $scope.callback = function(controls, $event){ // controls = {'user': 'Jon Doe'}, $event={formSubmitEvent}
      $http.post('saveDataUrl', controls);
  };

Optional attribute:

It is optional if the property is set to false globally

  auto-save-form-mode="boolean"

Trigger form submission, useful when the button is outside of the form

  $scope.autoSaveFormSubmit($event);

Global configuration

In config phase add autoSaveFormProvider

  autoSaveFormProvider.setDebounce(500); //change globaly default debounce timer
  autoSaveFormProvider.setAutoSaveMode(true); //change globaly default auto save mode
  autoSaveFormProvider.setSpinner(true); //change globaly default spinner
  autoSaveFormProvider.setSpinnerPosition('top right'); //change globaly default position of the spinner

License

The MIT License

Copyright (c) 2017 Tiberiu Zuld

angular-auto-save-form's People

Contributors

tiberiuzuld avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

angular-auto-save-form's Issues

Add optional loading spinner

  1. Optional if callback function returns promise Spinner in top right corner till promise resolves
  2. Config options for Spinner in all 4 corners ['top left', 'top right', 'bottom left', 'bottom right']
    defaults: top right
  3. Add a green check mark for 2-3 second after promise resolves
  4. Add a red X mark for 2-3 seconds after promise rejects

Minor change in README.md

Please change
<script src="bower_components/angular-auto-save-form/auto-save-form.js"></script>
to
<script src="bower_components/angular-auto-save-form/dist/auto-save-form.js"></script>

Also, please add underscore as a dependency.

Great job indeed!

Has this been tested with controllerAs controllers?

I'm using it in an angular 1 project that's using typescript and controllerAs pattern rather than $scope (see https://docs.angularjs.org/guide/concepts#controller). Things seem to work fine (aata gets auto saved) but I get this error when using angular auto save:

error_handler.js:51 TypeError: scope.$eval(...) is not a function at getChangedControls (auto-save-form.js:116) at invokeFunc (lodash.js:10024) at trailingEdge (lodash.js:10071) at timerExpired (lodash.js:10059) at ZoneDelegate.invokeTask (zone.js:225) at Object.onInvokeTask (ng_zone_impl.js:34) at ZoneDelegate.invokeTask (zone.js:224) at Zone.runTask (zone.js:125) at ZoneTask.invoke (zone.js:293)

If I switch to adding my auto save function to the $scope variable then that error no longer appears.

additional object

I have multiple forms on a page that all need their fields saved to their specific id. Can I pass an additional object to the callback?

reference error

Hi
I added the dependencies to my project and the attributes to the form. But when I load the page I get this error:

base.js:23068 ReferenceError: _ is not defined
at saveFormLink (http://localhost:3010/app/js/base.js:68202:26)
at invokeLinkFn (http://localhost:3010/app/js/base.js:19324:9)
at nodeLinkFn (http://localhost:3010/app/js/base.js:18810:11)
at compositeLinkFn (http://localhost:3010/app/js/base.js:18058:13)
at compositeLinkFn (http://localhost:3010/app/js/base.js:18061:13)
at nodeLinkFn (http://localhost:3010/app/js/base.js:18805:24)
at compositeLinkFn (http://localhost:3010/app/js/base.js:18058:13)
at publicLinkFn (http://localhost:3010/app/js/base.js:17938:30)
at http://localhost:3010/app/js/base.js:50541:9
at invokeLinkFn (http://localhost:3010/app/js/base.js:19324:9)

What am I missing?

Thanks

Make demo more fancy

  1. Make the demo page more fancy with material-design or better styling and animations.
  2. Make more complex examples.

Does this support radio buttons?

Something like:

<div>
    <input type="radio" ng-model="lunch" value="chicken" name="lunch">
    <input type="radio" ng-model="lunch" value="beef" name="lunch">
    <input type="radio" ng-model="lunch" value="fish" name="lunch">  
</div>

Doesn't look it. It seems to require name to be unique for each input.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.