can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
55 lines (52 loc) • 1.48 kB
JavaScript
steal('can/util', function (can) {
/**
* @typedef {{bind:function():*,unbind:function():*}} can.util.bind
*
* Provides mixin-able bind and unbind methods. `bind()` calls `this._bindsetup`
* when the first bind happens and. `unbind()` calls `this._bindteardown` when there
* are no more event handlers.
*
*/
// ## Bind helpers
can.bindAndSetup = function () {
// Add the event to this object
can.addEvent.apply(this, arguments);
// If not initializing, and the first binding
// call bindsetup if the function exists.
if (!this.__inSetup) {
if (!this._bindings) {
this._bindings = 1;
// setup live-binding
if (this._bindsetup) {
this._bindsetup();
}
} else {
this._bindings++;
}
}
return this;
};
can.unbindAndTeardown = function (event, handler) {
if (!this.__bindEvents) {
return this;
}
var handlers = this.__bindEvents[event] || [];
var handlerCount = handlers.length;
// Remove the event handler
can.removeEvent.apply(this, arguments);
if (this._bindings === null) {
this._bindings = 0;
} else {
// Subtract the difference in the number of handlers bound to this
// event before/after removeEvent
this._bindings = this._bindings - (handlerCount - handlers.length);
}
// If there are no longer any bindings and
// there is a bindteardown method, call it.
if (!this._bindings && this._bindteardown) {
this._bindteardown();
}
return this;
};
return can;
});