add that.update to Ox.Element

This commit is contained in:
rlx 2012-05-28 16:16:23 +00:00
parent d05a4d0849
commit ab68c4a54f

View file

@ -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 = $('<div>');
}
// 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 || '<div>'))
@ -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 <function> Binds a function to an event
@ -399,7 +399,7 @@ Ox.Element = function(options, self) {
value <val> 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 <f> Adds one or more handlers for options updates
(callback) -> <o> that
(key, callback) -> <o> that
({key: callback, ...}) -> <o> 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 <f> Shortcut to get or set self.options.value
@*/
@ -528,6 +549,10 @@ Ox.Element = function(options, self) {
);
};
that.update({
tooltip: setTooltip
});
return that;
};