Skip to content

Commit

Permalink
Added ability to trigger custom events in response to DOM insertions.
Browse files Browse the repository at this point in the history
Functionality, documentation, and showcasing example added.
  • Loading branch information
John Lianoglou committed Oct 5, 2014
1 parent eba1807 commit 1863216
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ $widget.whenLive({
$('body').prepend($widget);
```

### Triggering Custom Events

Finally, you can also trigger custom events in response to the addition of specific elements to the DOM by binding against the `document` object.

```javascript
// declare an event listener
$(document).bind("dropdown.added", function(event) {
var $dropdown = $(event.addedElement);
// whatever
});

$(".dropdown").whenLive('dropdown.added');
```

Of course you can use the same options when using custom events:

```javascript
$widget.whenLive({
'visibility': false
}, 'dropdown.added');
```

## Installation

### Bower
Expand All @@ -69,4 +91,3 @@ A guy walks into a bar and takes a seat. Before he can order a beer, the bowl of
### Notes

* If the targeted element already meets the specified criteria when the plugin is called, the callback function will be fired immediately.

20 changes: 19 additions & 1 deletion example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,26 @@ var example2 = function() {

}

var example3 = function() {

console.log('Running example 3...');

var $widget = $("<div class='widget'>I am a nobody. Nobody is perfect. Therefore, I am perfect.</div>");

$(".widget").whenLive('example.added');

$(document).bind('example.added', function(evt) {
console.log("foo.event fired with data %o", evt);
});

$('body').prepend($widget);

}

example1();
setTimeout(function() {
example2();
}, 4000);

setTimeout(function() {
example3();
}, 8000);
25 changes: 19 additions & 6 deletions src/jquery.whenlive.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
* Tim Ambler <[email protected]>
*/
(function(root, factory) {
if ( typeof define === 'function' && define.amd ) {
define(['jquery'], factory);
} else {
factory(jQuery);
}
if ( typeof define === 'function' && define.amd ) {
define(['jquery'], factory);
} else {
factory(jQuery);
}
})(this, function(jQuery) {

(function($) {
Expand Down Expand Up @@ -68,6 +68,9 @@
if ( typeof options === 'function' ) {
fn = options;
options = {};
} else if (typeof options === 'string' ) {
fn = options;
options = {};
} else if ( typeof options !== 'object' ) {
options = {};
}
Expand All @@ -76,6 +79,16 @@
options.visibility = true;
}

if ( typeof fn === 'string' ) {
var evtName = fn;
fn = function(el) {
$(document).trigger({
type: evtName,
element: el
});
}
}

if ( typeof fn !== 'function' ) {
return;
}
Expand All @@ -94,7 +107,7 @@
for ( var mi = 0; mi < mutations.length; mi++ ) {
var mutation = mutations[mi];
checkElements(mutation.target);
if ( $.whenLiveElements.length && mutation.addedNodes != null ) {
if ( $.whenLiveElements.length && mutation.addedNodes != null ) {
for ( var ni = 0; ni < mutation.addedNodes.length; ni ++ ) {
var node = mutation.addedNodes[ni];
checkElements(node);
Expand Down

0 comments on commit 1863216

Please sign in to comment.