The attach method of the behavior is called every time behaviors are attached. This causes three issues:
1) The uniqueness variable is repeated initialized. This should be addressed by the X = X || init_val pattern. It should probably also be explicitly declared in the outer scope (which should be a closure).
2) The title field's keyup method is repeatedly bound. This should be extended with the .once() pattern.
3) The tags field's blur method is repeated bound. Same as 2).
Additionally, uniqueness is in the global scope. The entire module should be enclosed in a $ closure, which would also allow all the jQuery references to be changed to the standard $ identifier and keep the global variables out of the global scope.
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | uniqueness-jquery_initialization_nowhitespacediff-1892868-1.patch | 3.53 KB | danchadwick |
| #1 | uniqueness-jquery_initialization-1892868-1.patch | 9.45 KB | danchadwick |
Comments
Comment #1
danchadwick commentedBecause encapsulating the file in a closure indents the whole file, I have included both a normal patch (intended to be applied) and one with -w (ignoring whitespace) so you can see the actual changes (other than indentation).
I have confirmed that this works both after the initial page load and when Drupal.attachBehaviors() is called.
Comment #2
danchadwick commentedComment #3
bforchhammer commentedAwesome, thanks! Cleaned up some whitespace issues and committed.