ferngully-aurelia-tools
Version:
Ferngully Tools for Aurelia
64 lines • 2.9 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { autoinject } from "aurelia-framework";
import { bindingMode as BindingMode } from "aurelia-binding";
var PersistentBindingBehavior = (function () {
function PersistentBindingBehavior() {
}
PersistentBindingBehavior.prototype.bind = function (binding, scope, delay, currentValue) {
if (delay === undefined) {
delay = 500;
}
var methodToThrottle = 'updateTarget';
if (binding.callSource) {
methodToThrottle = 'callSource';
}
else if (binding.updateSource && binding.mode === BindingMode.twoWay) {
methodToThrottle = 'updateSource';
}
binding.throttledMethod = binding[methodToThrottle];
binding.throttledMethod.originalName = methodToThrottle;
binding[methodToThrottle] = _delay;
binding.throttleState = {
delay: delay,
timeoutId: null,
currentValue: currentValue,
newValue: undefined
};
};
PersistentBindingBehavior.prototype.unbind = function (binding, scope) {
var methodToRestore = binding.throttledMethod.originalName;
binding[methodToRestore] = binding.throttledMethod;
binding.throttledMethod = null;
clearTimeout(binding.throttleState.timeoutId);
binding.throttleState = null;
};
PersistentBindingBehavior = __decorate([
autoinject,
__metadata("design:paramtypes", [])
], PersistentBindingBehavior);
return PersistentBindingBehavior;
}());
export { PersistentBindingBehavior };
function _delay(newValue) {
var state = this.throttleState;
var _this = this;
state.newValue = newValue;
if (!state.timeoutId && (state.newValue !== state.currentValue)) {
state.timeoutId = setTimeout(function () {
if (state.newValue !== state.currentValue) {
_this.throttledMethod(state.newValue);
state.currentValue = state.newValue;
}
state.timeoutId = 0;
}, state.delay);
}
}
//# sourceMappingURL=persistent.js.map