Benutzer:J*/Ka-Mel-Oh/Testgelände/Notizzettel
< Benutzer:J* | Ka-Mel-Oh | Testgelände
Zur Navigation springen
Zur Suche springen
Version vom 21. Februar 2009, 00:32 Uhr von J* (Diskussion | Beiträge) (Die Seite wurde neu angelegt.)
// Entwurf 1 für das Framework
Test = function (a) {
var x = this;
a.each( function(e) { x.push(e) } );
this.info="abc";
};
// Achtung! geht nur mit der Prototype-Library (Prototype.js - nicht verwechseln mit irgendwas.prototype!)
/* +-----------------------------------------------------------+
* | | * | Modul 1: Erweiterungen von Datentypen mittels prototype | * | | * +-----------------------------------------------------------+ */
/*
* Erweiterung von Array: Array.where erlaubt die Filterung von Objekten mittels Vergleichsfunktion
*
* Das benutze Array.each ist übrigens der for...in-Ersatz von Prototype
* (for...in funktionert nicht 100% ordentlich, wenn man prototype in Arrays benutzt)
*
* Beispiel für Array.where:
* var a = [1,2,3];
* a.where( function(e) { return(e>1) } )
* --> gibt zurück: [2,3]
*/
Array.prototype.where = function ( fkt ) {
var out = [];
this.each( function (e) {
if ( fkt(e) )
out.push(e)
} );
return out;
}
/* +-------------------------------------------------------------------------+
* | | * | Modul 2: Die Async-Klasse, um asynchrone Vorgänge behandeln zu können | * | mehr Erklärung gibt's woanders ... vielleicht ... | * | | * +-------------------------------------------------------------------------+ */
function Async ( arr, ondone ) {
var asyncobj = this;
arr.each( function(d) { asyncobj.push(d); } );
this.pointer = 0;
this.ondone = ondone;
this.nextElement = function() { return this[this.pointer+1]; };
this.currentElement = function() { return this[this.pointer]; };
this.start = function ( obj, ond ) {
if (ond != null)
{
this.ondone = ond;
}
this.pointer = 0;
this._control( obj );
};
this.continue = function ( obj ) {
this.pointer++;
this._control( obj );
};
this._control = function ( obj ) {
if (this.pointer >= this.length)
{
if (typeof(this.ondone)=="function")
{
alert("Return: Function")
this.ondone( obj, this );
return
}
if (typeof(this.ondone)=="object") // instanceof gibt hier nur ärger
{
alert("Return: object")
this.ondone.continue( obj );
return
}
}
if (typeof(this[this.pointer])=="function")
{
this.continue( this[this.pointer]( obj, this ) );
return
}
if (this[this.pointer] instanceof Async)
{
this[this.pointer].start( obj, this );
return
}
};
}
Async.prototype = new Array ();
// -----------------------------------------------------------------------------------------
ready = function (w) { alert(w.blubb) } ready2 = function (w) { alert(w.blubb+"..") }
x = new Async([
function(t) { t.blubb ++; return t },
function(t) { this.nextElement().push( function(u) { t.blubb = u.blubb + 7; return u; } ); return t},
new Async([
function(t) { t.blubb = t.blubb * 10; return t }
]),
function(z) { z.w = z.blubb; return z }
],ready);
x.start( { blubb:5 } );