From ab68c4a54f49fb319e2ea3057e4b6a5c6faffda2 Mon Sep 17 00:00:00 2001
From: rlx <0x0073@0x2620.org>
Date: Mon, 28 May 2012 16:16:23 +0000
Subject: [PATCH] add that.update to Ox.Element
---
source/Ox.UI/js/Core/Ox.Element.js | 47 +++++++++++++++++++++++-------
1 file changed, 36 insertions(+), 11 deletions(-)
diff --git a/source/Ox.UI/js/Core/Ox.Element.js b/source/Ox.UI/js/Core/Ox.Element.js
index 9da44fba..d1d9fde9 100644
--- a/source/Ox.UI/js/Core/Ox.Element.js
+++ b/source/Ox.UI/js/Core/Ox.Element.js
@@ -90,13 +90,15 @@ Ox.Element = function(options, self) {
element: self.options
};
}
- // array of callbacks bound to any event
- self.eventCallbacks = [];
// create event handler
// (this can be passed as part of self)
if (!self.$eventHandler) {
self.$eventHandler = $('
');
}
+ // array of callbacks bound to any event
+ self.eventCallbacks = [];
+ // stack of callbacks bound to option updates
+ self.updateCallbacks = [];
// create public object
var that = new Ox.JQueryElement($(self.options.element || '
'))
@@ -282,14 +284,12 @@ Ox.Element = function(options, self) {
}
}
- self.setOption = function(key, value) {
- // self.setOption(key, value)
- // is called when an option changes
- // (to be implemented by widget)
- if (key == 'tooltip') {
- setTooltip();
- }
- };
+ function update(key, value) {
+ // update is called whenever an option is modified or added
+ Ox.loop(self.updateCallbacks.length - 1, -1, -1, function(i) {
+ self.updateCallbacks[i](key, value) === false && Ox.Break();
+ });
+ }
/*@
bindEvent Binds a function to an event
@@ -399,7 +399,7 @@ Ox.Element = function(options, self) {
value the value of the option
@*/
that.options = function() {
- return Ox.getset(self.options, arguments, self.setOption, that);
+ return Ox.getset(self.options, arguments, update, that);
};
/*@
@@ -519,6 +519,27 @@ Ox.Element = function(options, self) {
return that;
};
+ /*@
+ update Adds one or more handlers for options updates
+ (callback) -> that
+ (key, callback) -> that
+ ({key: callback, ...}) -> that
+ @*/
+ that.update = function() {
+ var callbacks;
+ if (Ox.typeOf(arguments[0]) == 'function') {
+ self.updateCallbacks.push(arguments[0]);
+ } else {
+ callbacks = Ox.makeObject(arguments);
+ self.updateCallbacks.push(function(key) {
+ if (callbacks[key]) {
+ return callbacks[key]();
+ }
+ });
+ }
+ return that;
+ };
+
/*@
value Shortcut to get or set self.options.value
@*/
@@ -528,6 +549,10 @@ Ox.Element = function(options, self) {
);
};
+ that.update({
+ tooltip: setTooltip
+ });
+
return that;
};