Skip to content

Commit

Permalink
refactoring for 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
mmonteleone committed Jan 15, 2010
1 parent bdbe31f commit 70be810
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 200 deletions.
6 changes: 4 additions & 2 deletions README.markdown
Expand Up @@ -3,13 +3,15 @@ jQuery.netchanger
simple extensions to the DOM onchange event
[http://github.com/mmonteleone/jquery.netchanger][0]

**Support for jQuery's 1.4's now usable live() event coming soon.**

What?
----

The native `onchange` DOM event fires when a control loses the input focus and its value has been modified since gaining focus, and is useful for tracking changes to a form. While useful, it has limitations:

* It fires whether the new value was genuinely different than when it gained focus or not
* For example, a user who clicks into an empty text input, types some text, deletes it, and unfocuses still triggers an "onchange"
* It only knows if a change is relative to when the input gained focus, not relative to other events, like when the page loaded.
* For example, a user who clicks into a populated empty text input, changes it, blurs, then focuses, and changes it back... netchanger knows that a real change took place and was subsequently reverted.
* It only fires when the control loses focus

`jQuery.netchanger` is a simple jQuery plugin which provides extra events for detecting when a control's value has noticeably changed relative to its original value or when it has returned back to its original value from some other value. Moreover, these new events are raised not only when the control loses focus, but also as the value changes while it still has focus. This can be useful for various progressive form enhancements, such as visibly styling modified but not-yet-saved fields.
Expand Down
68 changes: 46 additions & 22 deletions jquery.netchanger.js
Expand Up @@ -50,30 +50,53 @@
* @param {Object} options optional object literal options
*/
netchanger: function(options){
var settings = $.extend({}, $.netchanger.defaults, options || {});

return this.each(function(){
var elm = $(this);
var existingValue = elm.data(valueKey);
// only set up netchanger on matched inputs that
// haven't already had netchanger applied to them
if(existingValue === notdefined) {
// capture current (initial) value
elm.data(valueKey,value(elm))
.bind(settings.events.replace(/,/g,' '), function(){
// bind to all specified events
// to check the current value and raise custom events
// when necessary
var initialValue = elm.data(valueKey);
if(value(elm) !== initialValue) {
elm.trigger('netchange');
}
if(value(elm) === initialValue) {
elm.trigger('revertchange');
}
});
}
// return this.each(function(){
// var elm = $(this);
// var existingValue = elm.data(valueKey);
// // only set up netchanger on matched inputs that
// // haven't already had netchanger applied to them
// if(existingValue === notdefined) {
// // capture current (initial) value
// elm.data(valueKey,value(elm))
// .bind(settings.events.replace(/,/g,' '), function(){
// // bind to all specified events
// // to check the current value and raise custom events
// // when necessary
// var initialValue = elm.data(valueKey);
// if(value(elm) !== initialValue) {
// elm.trigger('netchange');
// }
// if(value(elm) === initialValue) {
// elm.trigger('revertchange');
// }
// });
// }
// });

var settings = $.extend({}, $.netchanger.defaults, options || {}),
selection = this,
elm,
initialValue;
var binder = settings.live ? 'live' : 'bind';

selection[binder](settings.live ? 'focusin' : 'focus', function(){
elm = $(this);
if(elm.data(valueKey) === undefined) {
elm.data(valueKey, value(elm));
}
});

$.each(settings.events.split(','), function(){
selection[binder](String(this), function(e){
elm = $(e.target);
initialValue = elm.data(valueKey);
if(value(elm) !== initialValue) { elm.trigger('netchange'); }
if(value(elm) === initialValue) { elm.trigger('revertchange'); }
});
});

return selection;
},

/**
Expand Down Expand Up @@ -154,6 +177,7 @@
$.extend($.netchanger,{
version: '0.9.0',
defaults: {
live: false,
selector: 'input,select,textarea,fileupload',
events: 'change,keyup,paste'
}
Expand Down

0 comments on commit 70be810

Please sign in to comment.