cleanup and simplify Ox.JQueryElement

This commit is contained in:
rolux 2012-05-28 11:30:51 +02:00
parent 8210a0c5cd
commit 1b99ab0fc9

View file

@ -6,32 +6,30 @@ Ox.JQueryElement <f> Wrapper for jQuery
$element <object> jQuery DOM Element $element <object> jQuery DOM Element
@*/ @*/
Ox.JQueryElement = function($element) { Ox.JQueryElement = function($element) {
var that = this;
//@ id <number> Unique id //@ id <number> Unique id
that.oxid = Ox.uid(); this.oxid = Ox.uid();
//@ $element <object> The jQuery-wrapped DOM element //@ $element <object> The jQuery-wrapped DOM element
that.$element = $element.data({ this.$element = $element.data({
oxid: that.oxid oxid: this.oxid
}); });
//@ 0 <element> The DOM element //@ 0 <element> The DOM element (for compatibility with jQuery)
that[0] = that.$element[0]; this[0] = this.$element[0];
//@ length <number> 1 (for compatibility with jQuery) //@ length <number> 1 (for compatibility with jQuery)
that.length = 1; this.length = 1;
Ox.UI.elements[that.oxid] = that; Ox.UI.elements[this.oxid] = this;
return that; return this;
}; };
// add all jQuery functions to the prototype of Ox.JQueryElement // add all jQuery methods to the prototype of Ox.JQueryElement
Ox.methods($('<div>'), true).forEach(function(method) { Ox.methods($('<div>'), true).forEach(function(method) {
Ox.JQueryElement.prototype[method] = function() { Ox.JQueryElement.prototype[method] = function() {
var args = arguments, oxid, ret, that = this; var $element = this.$element[method].apply(this.$element, arguments),
ret = that.$element[method].apply(that.$element, args); oxid;
// if exactly one $element of an ox object was returned // if exactly one $element of an Ox object was returned, then return the
// then return the ox object instead // Ox object instead, so that we can do oxObj.jqFn().oxFn()
// so that we can do oxObj.jqFn().oxFn() return $element && $element.jquery
return ret && ret.jquery && $element.length == 1
&& ret.length == 1 && Ox.UI.elements[oxid = $element.data('oxid')]
&& Ox.UI.elements[oxid = ret.data('oxid')] ? Ox.UI.elements[oxid] : $element;
? Ox.UI.elements[oxid] : ret;
}; };
}); });