From 5a05da2af27071a9a137da278301e98c4dd6be59 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 4 Jan 2011 22:30:10 +0100 Subject: [PATCH] Some common javascript functions for working with arrays and functions the callBack class becomes obsolete with these but all places where it is used are updates --- js/lib_ajax.js | 66 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/js/lib_ajax.js b/js/lib_ajax.js index 297f612252..bdcf15d20e 100644 --- a/js/lib_ajax.js +++ b/js/lib_ajax.js @@ -20,11 +20,10 @@ */ //The callBack object provides an easy way to pass a member of an object as callback parameter and makes sure that the 'this' is always set correctly when called. +//bindScope provides a much cleaner sollution but we keep this one for compatibility and instead implement is with bindScope callBack=function(func,obj){ - this.id=callBack.callBacks.length; - callBack.callBacks[this.id]=this; - this.func=func; - this.obj=obj; + var newFunction=func.bindScope(obj); + callBack.callBacks[this.id]=newFunction; } callBack.callBacks=Array(); @@ -36,18 +35,6 @@ callBack.call=function(id,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10){ } } -callBack.prototype=function(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10){ - return this.call(false,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10); -} -callBack.prototype.func=false; -callBack.prototype.obj=false; -callBack.prototype.call=function(dummy,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10){ - return this.func.call(this.obj,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10); -} -callBack.prototype.apply=function(dummy,arguments){ - return this.func.apply(this.obj,arguments); -} - //provide a simple way to add things to the onload OC_onload=new Object(); @@ -198,4 +185,49 @@ loadScript=function(url){//dynamicly load javascript files script.setAttribute('src',url); body=document.getElementsByTagName('body').item(0); body.appendChild(script); -} \ No newline at end of file +} + +Function.prototype.bindScope=function(obj){ + var o=obj; + var fn=this; + return function(){ + return fn.apply(o,arguments); + } +} + +Function.prototype.bind=function(){ + var args = []; + var fn=this; + for (var n = 0; n < arguments.length; n++){ + args.push(arguments[n]); + } + return function (){ + var myargs = []; + for (var m = 0; m < arguments.length; m++){ + myargs.push(arguments[m]); + } + return fn.apply(this, args.concat(myargs)); + }; +} + +Array.prototype.foreach=function(func,that){ + if (!func) return; + that=that||this; + var returns=[]; + for(var i=0;i