Просмотр исходного кода

使用Promise支持表单的异步默认值

huqi 5 лет назад
Родитель
Сommit
2a9ea82a50

+ 47 - 34
o2web/source/o2_core/o2.js

@@ -1670,38 +1670,38 @@
             // }
     };
 
-    Object.appendChain = function(oChain, oProto) {
-        if (arguments.length < 2) {
-            throw new TypeError('Object.appendChain - Not enough arguments');
-        }
-        if (typeof oProto === 'number' || typeof oProto === 'boolean') {
-            throw new TypeError('second argument to Object.appendChain must be an object or a string');
-        }
-
-        var oNewProto = oProto,
-            oReturn,
-            o2nd,
-            oLast;
-
-        oReturn = o2nd = oLast = oChain instanceof this ? oChain : new oChain.constructor(oChain);
-
-        for (var o1st = this.getPrototypeOf(o2nd);
-             o1st !== Object.prototype && o1st !== Function.prototype;
-             o1st = this.getPrototypeOf(o2nd)
-        ) {
-            o2nd = o1st;
-        }
-
-        if (oProto.constructor === String) {
-            oNewProto = Function.prototype;
-            oReturn = Function.apply(null, Array.prototype.slice.call(arguments, 1));
-            oReturn = oReturn.bind(oLast);
-            this.setPrototypeOf(oReturn, oLast);
-        }
-
-        this.setPrototypeOf(o2nd, oNewProto);
-        return oReturn;
-    }
+    // Object.appendChain = function(oChain, oProto) {
+    //     if (arguments.length < 2) {
+    //         throw new TypeError('Object.appendChain - Not enough arguments');
+    //     }
+    //     if (typeof oProto === 'number' || typeof oProto === 'boolean') {
+    //         throw new TypeError('second argument to Object.appendChain must be an object or a string');
+    //     }
+    //
+    //     var oNewProto = oProto,
+    //         oReturn,
+    //         o2nd,
+    //         oLast;
+    //
+    //     oReturn = o2nd = oLast = oChain instanceof this ? oChain : new oChain.constructor(oChain);
+    //
+    //     for (var o1st = this.getPrototypeOf(o2nd);
+    //          o1st !== Object.prototype && o1st !== Function.prototype;
+    //          o1st = this.getPrototypeOf(o2nd)
+    //     ) {
+    //         o2nd = o1st;
+    //     }
+    //
+    //     if (oProto.constructor === String) {
+    //         oNewProto = Function.prototype;
+    //         oReturn = Function.apply(null, Array.prototype.slice.call(arguments, 1));
+    //         oReturn = oReturn.bind(oLast);
+    //         this.setPrototypeOf(oReturn, oLast);
+    //     }
+    //
+    //     this.setPrototypeOf(o2nd, oNewProto);
+    //     return oReturn;
+    // }
 
     // user promise
     // var _AsyncGeneratorPrototype = _Class.create({
@@ -1847,8 +1847,21 @@
     // };
 
     var _promiseAll = function(p){
-        var method = (o2.typeOf(p)=="array") ? "all" : "resolve";
-        return Promise[method](p);
+        if (o2.typeOf(p)=="array"){
+            if (p.some(function(e){ return (o2.typeOf(e.then)=="function") })){
+                return Promise.all(p);
+            }else{
+                return { "then": function(s){ s(p); return this;} };
+            }
+        }else{
+            if (o2.typeOf(p.then)=="function"){
+                return Promise.resolve(p);
+            }else{
+                return { "then": function(s){ s(p); return this;} };
+            }
+        }
+        // var method = (o2.typeOf(p)=="array") ? "all" : "resolve";
+        // return Promise[method](p);
     }
     o2.promiseAll = _promiseAll;
 

+ 2 - 2
o2web/source/o2_core/o2/widget/O2Identity.js

@@ -284,7 +284,7 @@ o2.widget.O2Duty = new Class({
     getPersonData: function(){
         if (!this.data.woUnit){
             this.action.actions = {"getUnitduty": {"uri": "/jaxrs/unitduty/{id}"}};
-            this.action.invoke({"name": "getUnitduty", "async": false, "parameter": {"id": (this.data.dutyId || this.data.name)}, "success": function(json){
+            this.action.invoke({"name": "getUnitduty", "async": false, "parameter": {"id": (this.data.id || this.data.name)}, "success": function(json){
                 this.data = json.data;
             }.bind(this)});
         }
@@ -727,4 +727,4 @@ o2.widget.O2Org = function(value, container, options){
 //             }
 //         });
 //     }
-// }, 10000);
+// }, 10000);

+ 23 - 12
o2web/source/x_component_process_Xform/$Input.js

@@ -201,13 +201,19 @@ MWF.xApplication.process.Xform.$Input = MWF.APP$Input =  new Class({
         //         this.moduleValueAG = null;
         //     }.bind(this));
         // }else {
-        var p = o2.promiseAll(value).then(function(v){
-            this.__setValue(v);
-        }.bind(this));
-        this.moduleValueAG = p;
-        p.then(function(){
+        if (o2.typeOf(value.then)=="function"){
+            var p = o2.promiseAll(value).then(function(v){
+                this.__setValue(v);
+            }.bind(this));
+            this.moduleValueAG = p;
+            p.then(function(){
+                this.moduleValueAG = null;
+            }.bind(this));
+        }else{
             this.moduleValueAG = null;
-        }.bind(this));
+            this.__setValue(v);
+        }
+
             //this.__setValue(value);
         // }
 
@@ -279,13 +285,18 @@ MWF.xApplication.process.Xform.$Input = MWF.APP$Input =  new Class({
         //         this.moduleValueAG = null;
         //     }.bind(this));
         // }else{
-        var p = o2.promiseAll(data).then(function(v){
-            this.__setValue(v);
-        }.bind(this));
-        this.moduleValueAG = p;
-        p.then(function(){
+        if (o2.typeOf(value.then)=="function"){
+            var p = o2.promiseAll(data).then(function(v){
+                this.__setValue(v);
+            }.bind(this));
+            this.moduleValueAG = p;
+            p.then(function(){
+                this.moduleValueAG = null;
+            }.bind(this));
+        }else{
             this.moduleValueAG = null;
-        }.bind(this));
+            this.__setValue(v);
+        }
             //this.__setData(data);
         //}
 	},

+ 9 - 3
o2web/source/x_component_process_Xform/Org.js

@@ -132,7 +132,9 @@ MWF.xApplication.process.Xform.Org = MWF.APPOrg =  new Class({
                                     if (dd) arr.push(MWF.org.parseOrgData(dd, true, simple));
                                 });
                                 return arr;
-                            }.bind(this));
+                            }.bind(this)).catch(function(){
+                                console.log("catch error : get duty")
+                            });
                         }.bind(this));
                         values.push(promise);
                     }
@@ -1033,9 +1035,13 @@ MWF.xApplication.process.Xform.Org = MWF.APPOrg =  new Class({
                 da.each(function(dd){
                     if (dd){
                         if (typeOf(dd)==="string"){
-                            ags.push(this.getOrgAction()[this.getValueMethod(dd)](function(json){
+                            var pp = this.getOrgAction()[this.getValueMethod(dd)](function(json){
                                 return MWF.org.parseOrgData(json.data, true, simple);
-                            }.bind(this), null, dd, true));
+                            }.bind(this), null, dd, true).catch(function(e){
+                                console.log("error:" + e);
+                                console.log(e);
+                            });
+                            ags.push(pp);
                         }else{
                             values.push(dd);
                         }