summaryrefslogtreecommitdiffstats
path: root/js
diff options
context:
space:
mode:
authorRobin Appelman <icewind1991@gmail.com>2011-01-04 22:30:10 +0100
committerRobin Appelman <icewind1991@gmail.com>2011-01-04 22:40:48 +0100
commit5a05da2af27071a9a137da278301e98c4dd6be59 (patch)
treec5009f78d775c5a613c9260659de44a2985ac8d5 /js
parent7710dc7325f15b4b8fd9488c31288ed5d6f79dac (diff)
downloadnextcloud-server-5a05da2af27071a9a137da278301e98c4dd6be59.tar.gz
nextcloud-server-5a05da2af27071a9a137da278301e98c4dd6be59.zip
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
Diffstat (limited to 'js')
-rw-r--r--js/lib_ajax.js66
1 files changed, 49 insertions, 17 deletions
diff --git a/js/lib_ajax.js b/js/lib_ajax.js
index 297f612252b..bdcf15d20e4 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<this.length;i++){
+ returns.push(func.call(that,this[i]));
+ }
+ return returns;
+}
+
+Array.prototype.where = function(func,that) {
+ var found = [];
+ that=that||this;
+ for(var i = 0, l = this.length; i < l; ++i) {
+ var item = this[i];
+ if(func.call(that,item)){
+ found.push(item);
+ }
+ }
+ return found;
+}; \ No newline at end of file