Giter Club home page Giter Club logo

angular-autodisable's Introduction

angular-autodisable Build Status

An extension to angular ng-click directive that automatically sets the element to disabled if the handler would return a promise.

Requirements


  1. angular.js version >1.2+
  2. following es5 functions:
    1. bind
    2. map
    3. forEach
    4. indexOf

Usage


Include ngAutodisable as dependency

  angular.module('MyApp', [ 'ngAutodisable', ... ]);

If that's done then just follow those simple steps:

  1. just attach ng-autodisable directive to the element which happens to have ng-click directive OR a form that has the ng-submit directive.
  2. ???
  3. profit!

On an element

  <button ng-click="doSomething()" ng-autodisable>Do something</button>
  
  <a ng-click="doSomething()" ng-autodisable>Do something</a>

On a form

  <form ng-submit="doSomething()" ng-autodisable> 
    ...
    <button type="submit">Submit</button> 
  </form>

The button with type submit within the form will be disabled.

Loading class

You can optionally add a list of classes which will be added to the element while this is disabled. This is useful to add a spinner or something similar.

  <button ng-click="doSomething()" 
          ng-autodisable-class="class1 class2" 
          ng-autodisable>
    Do something
  </button>
  
  <form ng-submit="doSomething()" 
        ng-autodisable-class="class1 class2"
        ng-autodisable> 
    ...
    <button type="submit">Submit</button> 
  </form>

The button with type submit within the form will get the class.

Demo


A quick demo is available at jsfiddle

How it works


When ngClick and ngAutodisable are on the same element then ngAutodisable overwrites the handler for click event. The default ngClick action is recreated (and passes all the angular specs).

If the click handlers result happens to be a promise ($http or $q) then the element attribute disabled will be set as true. If the promise fulfills then the element disabled attribute will be removed.

This also works with multiple click handlers, given that click handlers are separated by ; as such:

  <button ng-click="doSomething();doSomethingElse()" ng-autodisable>Do something</button>

If there are multiple click handlers then the element disabled style will be removed after the last promise resolves.

Note


Throws an exception ngAutodisable requires ngClick attribute in order to work if ngAutodisable is on an element without ngClick.

Devel


  npm install
  bower install
  grunt test
  grunt build

License


MIT

angular-autodisable's People

Contributors

aswin-s avatar dflourusso avatar floftar avatar kirstein avatar loicmahieu avatar stnor avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

angular-autodisable's Issues

jqLite's find() is limited to lookups by tag name

therefore, this snippet never finds any buttons to disable

          handler = handlerInstance(element.find('button[type=submit]'),
              SUBMIT_EVENT,
              getLoadingClass(attrs),
              getCallbacks(attrs[SUBMIT_ATTR]));

maybe you could use something like element[0].querySelectorAll('button[type=submit]') instead?

Click event registered on forms

When using the directive with forms it looks like a click event is being registered. When I click into my form, the ng-submit handler is invoked.

My HTML:

      <form name="formMenuItem" ng-submit="vm.saveMenuItem()" ng-autodisable>
        <button class="btn btn-primary" type="submit" ng-disabled="formMenuItem.$invalid">
          <span ng-if="vm.menuItem.id">Add Menu Item</span>
          <i class="icon icon-tick"></i>
        </button>
      </form>

Controller:

    function saveMenuItem() {
      return vm.menuItem.id ? updateMenuItem() : addMenuItem();
    }

    function addMenuItem() {
      return menuService.addMenuItem(menu, vm.menuItem).then(function (mi) {
        menu.items.push(mi);
        notify.success('Menu Item added successfully.');
        resetMenuItemForm();
      });
    }

Change ng-* prefix

The prefix ng-* in directive might conflict with future native directives.

'disabled' attribute is ineffective on an 'a' element

Hello,
First, thank you very much for this great module. I use it in my new project and it is very useful and convenient.
I think the following pattern is commonly used :

<a href="" ng-click="myFunction()">Myfunction</a>

With angular-autodisable, it would be useful to write that:

<a href="" ng-click="myFunction()" ng-autodisable>Myfunction</a>

Unfortunately, it does not work, since the disabled attribute does not forbid the ng-click to occur.
I made a fix for my project: I get the element to disable as a parameter of triggerHandler and I immediatly return in triggerHandler if this element has the disabled attribute.

autodisable form submit button

I'm trying to use this directive on a form with a button[type="submit"]. The problem is...

The child button is generated in a directive itself. (<md-button type="submit">Submit</md-button).

This is likely an issue where the child button is processed after the parent ng-autodisable linking function has run. Any suggestions?

does not work within a transclude directive.

a tranclude directive will clone its children element, that would make the elementToDisalbe in handlerInstance different from real element. so the disable won't work.

I think you should do the handlerInstance within linkFn instead of compile,

need to config when to disable.

when I submit a form in $modal(with animation: true), if I click very fast, the form will submit twice.
Because when the promise is done, the disabled is set to false, but the animation is not completed, I can still submit the form if I click very fast.
so, I want to config when to set disable to false.
In this scenorio, I want just when the promise is success, I set the disable to fase.

handling ng-disabled

One might want to disable / enable form inputs depending on some state. Currently, ng-autodisable and ng-disabled don't work well together, and we could have the situation where ng-autodisable has disabled an input, but a digest caused ng-disabled to reenable it while promise is not resolved yet.

Maybe there could be a simple / clever way to handle this.

not work with ngTouch module.

cause ngTouch module's ngClick directive using touch event to trigger click event. and disabled element can trigger touch event. so this won't work.

I can send a PR to fix this issue, does anyone maintain this project ?

Not working with ng-submit

Here is my jade template

form(name='signInForm' ng-submit="signIn()" ng-autodisable)
  input(type="email" ng-model="email" placeholder='Email' ng-required='true')
  input(type="password" ng-model="password" placeholder='Password' ng-required='true')
  button(type="submit" ng-disabled="signInForm.$invalid") Sign In

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.