diff --git a/src/ui.js b/src/ui.js index 8b978f5..3f5b304 100644 --- a/src/ui.js +++ b/src/ui.js @@ -140,6 +140,48 @@ Ply.ui = (function ($) { }, + // Declare method for binding events. Method is created for same purposes + // as `this.__bindElements`, `this.__bindPartials` and `this.__bindNotifications. + // An example key from `this.__events` could be 'click closeButton' + // which would refer to the click event for `this.objects.closeButton` + // an example value could be 'close', which would refer to the function `this.close` + __bindEvents: function () { + + var keys, + elem, + handler; + + // #### Events + + // If `this.__events` is defined, autogenerate the events. + if (this.__events) { + + // Iterate over the own properties of `this.__events`. + for (var id in this.__events) { + if (this.__events.hasOwnProperty(id)) { + + // Split the id into the handler and the element(s) + keys = id.split(' '); + // and asign them accordingly + handler = keys[0]; + elem = keys[1]; + + // Attach the event to the elem, but delegate it to this.view + this.view.on(handler, this.__elements[elem], (function (callback, self) { + return function (e) { + // Within the callback, `this` refers to the Ply view, + // not the element the event is attached to, + // so we will pass that element in as a second argument + // for easy access + self[callback](e, e.target); + }; + }(this.__events[id], this))); + } + } + } + + }, + // Declare method to destroy view. `this.__destroyView` is automatically called when view element is // removed from the DOM. __destroyView: function () { @@ -205,6 +247,8 @@ Ply.ui = (function ($) { } } + // Events aren't stored anywhere other than on the nodes they're bound to, so these will be removed by $.cleanData + }, // See getOptionOrData @@ -275,6 +319,9 @@ Ply.ui = (function ($) { // Invoke `this.__bindElements`. this.__bindElements(); + // Invoke `this.__bindEvents`. + this.__bindEvents(); + // Invoke `this.__bindPartials`. this.__bindPartials(); @@ -496,4 +543,4 @@ Ply.ui = (function ($) { // Alias `jQuery` to `$` in the module's scope. })(jQuery); -// ↪ [Config](config.html) \ No newline at end of file +// ↪ [Config](config.html)