Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrapper for jQuery. #33

Merged
merged 2 commits into from
Apr 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ module.exports = function(grunt) {
preserveComments: 'some'
},
files: {
'dist/spin.min.js': 'js/spin.js'
'dist/spin.min.js': 'js/spin.js',
'dist/ladda.jquery.min.js': 'js/ladda.jquery.js'
}
}
},
Expand Down Expand Up @@ -67,7 +68,7 @@ module.exports = function(grunt) {
define: false
}
},
files: [ 'Gruntfile.js', 'js/ladda.js' ]
files: [ 'Gruntfile.js', 'js/ladda.js', 'js/ladda.jquery.js' ]
},

connect: {
Expand Down
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,46 @@ All loading animations on the page can be stopped by using:
Ladda.stopAll();
```

#### With jQuery

If you will be using the loading animation for a form that is submitted to the server (always resulting in a page reload) you can use the ```ladda('bind')``` method:

```javascript
// Automatically trigger the loading animation on click
$( 'input[type=submit]' ).ladda( 'bind' );

// Same as the above but automatically stops after two seconds
$( 'input[type=submit]' ).ladda( 'bind', { timeout: 2000 } );
```

If you want JavaScript control over your buttons you can use the following approach:

```javascript
// Create a new instance of ladda for the specified button
var l = $( '.my-button' ).ladda();

// Start loading
l.ladda( 'start' );

// Will display a progress bar for 50% of the button width
l.ladda( 'setProgress', 0.5 );

// Stop loading
l.ladda( 'stop' );

// Toggle between loading/not loading states
l.ladda( 'toggle' );

// Check the current state
l.ladda( 'isLoading' );
```

All loading animations on the page can be stopped by using:

```javascript
$.ladda( 'stopAll' );
```

## Module

The spinner and Ladda can be loaded as a module using either Common.js or AMD.
Expand Down
8 changes: 8 additions & 0 deletions dist/ladda.jquery.min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*!
* Ladda for jQuery
* http://lab.hakim.se/ladda
* MIT licensed
*
* Copyright (C) 2013 Hakim El Hattab, http://hakim.se
*/
(function(t,e){if(void 0===e)return console.error("jQuery required for Ladda.jQuery");var i=[];e=e.extend(e,{ladda:function(e){"stopAll"===e&&t.stopAll()}}),e.fn=e.extend(e.fn,{ladda:function(n){var r=i.slice.call(arguments,1);return"bind"===n?(r.unshift(e(this).selector),t.bind.apply(t,r)):e(this).each(function(){var i,o=e(this);void 0===n?o.data("ladda",t.create(this)):(i=o.data("ladda"),i[n].apply(i,r))}),this}})})(this.Ladda,this.jQuery);
46 changes: 46 additions & 0 deletions js/ladda.jquery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*!
* Ladda for jQuery
* http://lab.hakim.se/ladda
* MIT licensed
*
* Copyright (C) 2013 Hakim El Hattab, http://hakim.se
*/

(function( Ladda, $ ) {
if ($ === undefined)
return console.error( 'jQuery required for Ladda.jQuery' );

var arr = [];

$ = $.extend( $, {
ladda: function( arg ) {
if( arg === 'stopAll' )
Ladda.stopAll();
}
});

$.fn = $.extend( $.fn, {
ladda: function( arg ) {
var args = arr.slice.call( arguments, 1 );

if( arg === 'bind' ) {
args.unshift( $( this ).selector );
Ladda.bind.apply( Ladda, args );
}
else {
$( this ).each( function() {
var $this = $( this ), ladda;

if( arg === undefined )
$this.data( 'ladda', Ladda.create( this ) );
else {
ladda = $this.data( 'ladda' );
ladda[arg].apply( ladda, args );
}
});
}

return this;
}
});
}( this.Ladda, this.jQuery ));
Loading