Permalink
Browse files

MDL-35673 Improve performance of formchangechecker event creation

  • Loading branch information...
1 parent 5d6285c commit d6418e531dcb02db6a98fc88c76c7c7bf2834b61 @andrewnicols andrewnicols committed Sep 28, 2012
Showing with 18 additions and 10 deletions.
  1. +18 −10 lib/yui/formchangechecker/formchangechecker.js
@@ -11,21 +11,26 @@ YUI.add('moodle-core-formchangechecker',
}
Y.extend(FORMCHANGECHECKER, Y.Base, {
+
+ // The delegated listeners we need to detach after the initial value has been stored once
+ initialvaluelisteners : [],
+
/**
* Initialize the module
*/
initializer : function(config) {
var formid = 'form#' + this.get('formid');
// Add change events to the form elements
- Y.all(formid + ' input').once('change', M.core_formchangechecker.set_form_changed, this);
- Y.all(formid + ' textarea').once('change', M.core_formchangechecker.set_form_changed, this);
- Y.all(formid + ' select').once('change', M.core_formchangechecker.set_form_changed, this);
+ var currentform = Y.one(formid);
+ currentform.delegate('change', M.core_formchangechecker.set_form_changed, 'input', this);
+ currentform.delegate('change', M.core_formchangechecker.set_form_changed, 'textarea', this);
+ currentform.delegate('change', M.core_formchangechecker.set_form_changed, 'select', this);
// Add a focus event to check for changes which are made without triggering a change event
- Y.all(formid + ' input').on('focus', this.store_initial_value, this);
- Y.all(formid + ' textarea').on('focus', this.store_initial_value, this);
- Y.all(formid + ' select').on('focus', this.store_initial_value, this);
+ this.initialvaluelisteners.push(currentform.delegate('focus', this.store_initial_value, 'input', this));
+ this.initialvaluelisteners.push(currentform.delegate('focus', this.store_initial_value, 'textarea', this));
+ this.initialvaluelisteners.push(currentform.delegate('focus', this.store_initial_value, 'select', this));
// We need any submit buttons on the form to set the submitted flag
Y.one(formid).on('submit', M.core_formchangechecker.set_form_submitted, this);
@@ -52,9 +57,12 @@ YUI.add('moodle-core-formchangechecker',
// we no longer need to call this function
var formid = 'form#' + this.get('formid');
- Y.all(formid + ' input').detach('focus', this.store_initial_value, this);
- Y.all(formid + ' textarea').detach('focus', this.store_initial_value, this);
- Y.all(formid + ' select').detach('focus', this.store_initial_value, this);
+ // Detach all listen events to prevent duplicate initial value setting
+ var thisevent;
+ while (thisevent = this.initialvaluelisteners.shift()) {
+ thisevent.detach();
+ }
+
return;
}
@@ -176,6 +184,6 @@ YUI.add('moodle-core-formchangechecker',
};
},
'@VERSION@', {
- requires : ['base']
+ requires : ['base', 'event-focus']
}
);

0 comments on commit d6418e5

Please sign in to comment.