Преглед изворни кода

优化namespace 时对vc.on 做处理

wuxw пре 6 година
родитељ
комит
49b5dba003

+ 30 - 29
WebService/src/main/resources/components/floorPackage/floor-select2/floorSelect2.js

@@ -1,40 +1,41 @@
-(function(vc){
+(function (vc) {
     vc.extends({
     vc.extends({
         propTypes: {
         propTypes: {
-               parentModal:vc.propTypes.string
+            parentModal: vc.propTypes.string
         },
         },
-        data:{
-            floorSelect2Info:{
-                floors:[],
-                floorId:'-1',
-                floorNum:'',
-                floorName:'',
+        data: {
+            floorSelect2Info: {
+                floors: [],
+                floorId: '-1',
+                floorNum: '',
+                floorName: '',
             }
             }
         },
         },
-        watch:{
-            floorSelect2Info:{
+        watch: {
+            floorSelect2Info: {
                 deep: true,
                 deep: true,
-                handler:function(){
-                    vc.emit('unitSelect2', "transferFloor" ,this.floorSelect2Info);
+                handler: function () {
+                    vc.emit($namespace, 'unitSelect2', "transferFloor", this.floorSelect2Info);
                 }
                 }
             }
             }
         },
         },
-        _initMethod:function(){
+        _initMethod: function () {
             this._initFloorSelect2();
             this._initFloorSelect2();
         },
         },
-        _initEvent:function(){
+        _initEvent: function () {
             //监听 modal 打开
             //监听 modal 打开
-           /* $('#'+$props.parentModal).on('show.bs.modal', function () {
-                 vc.component._initFloorSelect2();
-            })*/
-           vc.on('floorSelect2','setFloor',function (_param) {
-               $("#floorSelector").val(_param.floorId).select2();
-           });
+            /* $('#'+$props.parentModal).on('show.bs.modal', function () {
+                  vc.component._initFloorSelect2();
+             })*/
+            vc.on('floorSelect2', 'setFloor', function (_param) {
+                $("#floorSelector").val(_param.floorId).select2();
+            });
         },
         },
         methods: {
         methods: {
             _initFloorSelect2: function () {
             _initFloorSelect2: function () {
                 console.log("调用_initFloorSelect2 方法");
                 console.log("调用_initFloorSelect2 方法");
-                $.fn.modal.Constructor.prototype.enforceFocus = function () {};
+                $.fn.modal.Constructor.prototype.enforceFocus = function () {
+                };
                 $.fn.select2.defaults.set('width', '100%');
                 $.fn.select2.defaults.set('width', '100%');
                 $('#floorSelector').select2({
                 $('#floorSelector').select2({
                     placeholder: '必填,请选择楼栋',
                     placeholder: '必填,请选择楼栋',
@@ -45,14 +46,14 @@
                         data: function (params) {
                         data: function (params) {
                             console.log("param", params);
                             console.log("param", params);
                             var _term = "";
                             var _term = "";
-                            if(params.hasOwnProperty("term")){
+                            if (params.hasOwnProperty("term")) {
                                 _term = params.term;
                                 _term = params.term;
                             }
                             }
                             return {
                             return {
                                 floorName: _term,
                                 floorName: _term,
                                 page: 1,
                                 page: 1,
-                                row:10,
-                                communityId:vc.getCurrentCommunity().communityId
+                                row: 10,
+                                communityId: vc.getCurrentCommunity().communityId
                             };
                             };
                         },
                         },
                         processResults: function (data) {
                         processResults: function (data) {
@@ -67,7 +68,7 @@
                 $('#floorSelector').on("select2:select", function (evt) {
                 $('#floorSelector').on("select2:select", function (evt) {
                     //这里是选中触发的事件
                     //这里是选中触发的事件
                     //evt.params.data 是选中项的信息
                     //evt.params.data 是选中项的信息
-                    console.log('select',evt);
+                    console.log('select', evt);
                     this.floorSelect2Info.floorId = evt.params.data.id;
                     this.floorSelect2Info.floorId = evt.params.data.id;
                     this.floorSelect2Info.floorName = evt.params.data.text;
                     this.floorSelect2Info.floorName = evt.params.data.text;
                 });
                 });
@@ -75,16 +76,16 @@
                 $('#floorSelector').on("select2:unselect", function (evt) {
                 $('#floorSelector').on("select2:unselect", function (evt) {
                     //这里是取消选中触发的事件
                     //这里是取消选中触发的事件
                     //如配置allowClear: true后,触发
                     //如配置allowClear: true后,触发
-                    console.log('unselect',evt)
+                    console.log('unselect', evt)
 
 
                 });
                 });
             },
             },
-            _filterFloorData:function (_floors) {
+            _filterFloorData: function (_floors) {
                 var _tmpFloors = [];
                 var _tmpFloors = [];
                 for (var i = 0; i < _floors.length; i++) {
                 for (var i = 0; i < _floors.length; i++) {
                     var _tmpFloor = {
                     var _tmpFloor = {
-                        id:_floors[i].floorId,
-                        text:_floors[i].floorName
+                        id: _floors[i].floorId,
+                        text: _floors[i].floorName
                     };
                     };
                     _tmpFloors.push(_tmpFloor);
                     _tmpFloors.push(_tmpFloor);
                 }
                 }

+ 4 - 0
WebService/src/main/resources/components/machinePackage/add-machine/addMachine.html

@@ -78,6 +78,8 @@
                                            parentModal="addMachineModel"
                                            parentModal="addMachineModel"
                                            callBackListener="addMachine"
                                            callBackListener="addMachine"
                                            callBackFunction="notify"
                                            callBackFunction="notify"
+                                           namespace="addMachine"
+
                                 ></vc:create>
                                 ></vc:create>
                             </div>
                             </div>
                         </div>
                         </div>
@@ -89,6 +91,8 @@
                                            parentModal="addMachineModel"
                                            parentModal="addMachineModel"
                                            callBackListener="addMachine"
                                            callBackListener="addMachine"
                                            callBackFunction="notify"
                                            callBackFunction="notify"
+                                           namespace="addMachine"
+
                                 ></vc:create>
                                 ></vc:create>
                             </div>
                             </div>
                         </div>
                         </div>

+ 10 - 10
WebService/src/main/resources/components/roomPackage/room-select2/roomSelect2.js

@@ -18,23 +18,23 @@
             roomSelect2Info:{
             roomSelect2Info:{
                 deep: true,
                 deep: true,
                 handler:function(){
                 handler:function(){
-                    vc.emit($props.callBackListener,$props.callBackFunction,vc.component.roomSelect2Info);
+                    vc.emit($props.callBackListener,$props.callBackFunction,this.roomSelect2Info);
                 }
                 }
             }
             }
         },
         },
         _initMethod:function(){
         _initMethod:function(){
-                vc.component._initRoomSelect2();
+                this._initRoomSelect2();
         },
         },
         _initEvent:function(){
         _initEvent:function(){
             //监听 modal 打开
             //监听 modal 打开
            /* $('#'+$props.parentModal).on('show.bs.modal', function () {
            /* $('#'+$props.parentModal).on('show.bs.modal', function () {
-                 vc.component._initUnitSelect2();
+                 this._initUnitSelect2();
             })*/
             })*/
            vc.on('roomSelect2', "transferRoom",function (_param) {
            vc.on('roomSelect2', "transferRoom",function (_param) {
-                vc.copyObject(_param, vc.component.roomSelect2Info);
+                vc.copyObject(_param, this.roomSelect2Info);
            });
            });
             vc.on('roomSelect2','setRoom',function (_param) {
             vc.on('roomSelect2','setRoom',function (_param) {
-                vc.copyObject(_param, vc.component.roomSelect2Info);
+                vc.copyObject(_param, this.roomSelect2Info);
                 $("#roomSelector").val(_param.roomId).select2();
                 $("#roomSelector").val(_param.roomId).select2();
             });
             });
         },
         },
@@ -59,14 +59,14 @@
                                 roomNum: _term,
                                 roomNum: _term,
                                 page: 1,
                                 page: 1,
                                 row:10,
                                 row:10,
-                                unitId:vc.component.roomSelect2Info.unitId,
+                                unitId:this.roomSelect2Info.unitId,
                                 communityId:vc.getCurrentCommunity().communityId
                                 communityId:vc.getCurrentCommunity().communityId
                             };
                             };
                         },
                         },
                         processResults: function (data) {
                         processResults: function (data) {
-                            console.log(data, vc.component._filterRoomData(data.rooms));
+                            console.log(data, this._filterRoomData(data.rooms));
                             return {
                             return {
-                                results: vc.component._filterRoomData(data.rooms)
+                                results: this._filterRoomData(data.rooms)
                             };
                             };
                         },
                         },
                         cache: true
                         cache: true
@@ -76,8 +76,8 @@
                     //这里是选中触发的事件
                     //这里是选中触发的事件
                     //evt.params.data 是选中项的信息
                     //evt.params.data 是选中项的信息
                     console.log('select',evt);
                     console.log('select',evt);
-                    vc.component.roomSelect2Info.roomId = evt.params.data.id;
-                    vc.component.roomSelect2Info.roomNum = evt.params.data.text;
+                    this.roomSelect2Info.roomId = evt.params.data.id;
+                    this.roomSelect2Info.roomNum = evt.params.data.text;
                 });
                 });
 
 
                 $('#roomSelector').on("select2:unselect", function (evt) {
                 $('#roomSelector').on("select2:unselect", function (evt) {

+ 42 - 41
WebService/src/main/resources/components/unitPackage/unit-select2/unitSelect2.js

@@ -1,48 +1,49 @@
-(function(vc){
+(function (vc) {
     vc.extends({
     vc.extends({
         propTypes: {
         propTypes: {
-            parentModal:vc.propTypes.string,
-            callBackListener:vc.propTypes.string, //父组件名称
-            callBackFunction:vc.propTypes.string //父组件监听方法
+            parentModal: vc.propTypes.string,
+            callBackListener: vc.propTypes.string, //父组件名称
+            callBackFunction: vc.propTypes.string //父组件监听方法
         },
         },
-        data:{
-            unitSelect2Info:{
-                units:[],
-                floorId:'-1',
-                unitId:'-1',
-                unitNum:'',
-                unitName:'',
+        data: {
+            unitSelect2Info: {
+                units: [],
+                floorId: '-1',
+                unitId: '-1',
+                unitNum: '',
+                unitName: '',
             }
             }
         },
         },
-        watch:{
-            unitSelect2Info:{
+        watch: {
+            unitSelect2Info: {
                 deep: true,
                 deep: true,
-                handler:function(){
-                    vc.emit($props.callBackListener,$props.callBackFunction,vc.component.unitSelect2Info);
-                    vc.emit('roomSelect2', "transferRoom" ,vc.component.unitSelect2Info);
+                handler: function () {
+                    vc.emit($props.callBackListener, $props.callBackFunction, this.unitSelect2Info);
+                    vc.emit($namespace, 'roomSelect2', "transferRoom", this.unitSelect2Info);
                 }
                 }
             }
             }
         },
         },
-        _initMethod:function(){
-                vc.component._initUnitSelect2();
+        _initMethod: function () {
+            this._initUnitSelect2();
         },
         },
-        _initEvent:function(){
+        _initEvent: function () {
             //监听 modal 打开
             //监听 modal 打开
-           /* $('#'+$props.parentModal).on('show.bs.modal', function () {
-                 vc.component._initUnitSelect2();
-            })*/
-           vc.on('unitSelect2', "transferFloor",function (_param) {
-                vc.copyObject(_param, vc.component.unitSelect2Info);
-           });
-            vc.on('unitSelect2','setUnit',function (_param) {
-                vc.copyObject(_param, vc.component.unitSelect2Info);
+            /* $('#'+$props.parentModal).on('show.bs.modal', function () {
+                  this._initUnitSelect2();
+             })*/
+            vc.on('unitSelect2', "transferFloor", function (_param) {
+                vc.copyObject(_param, this.unitSelect2Info);
+            });
+            vc.on('unitSelect2', 'setUnit', function (_param) {
+                vc.copyObject(_param, this.unitSelect2Info);
                 $(".unitSelector").val(_param.unitId).select2();
                 $(".unitSelector").val(_param.unitId).select2();
             });
             });
         },
         },
         methods: {
         methods: {
             _initUnitSelect2: function () {
             _initUnitSelect2: function () {
                 console.log("调用_initUnitSelect2 方法");
                 console.log("调用_initUnitSelect2 方法");
-                $.fn.modal.Constructor.prototype.enforceFocus = function () {};
+                $.fn.modal.Constructor.prototype.enforceFocus = function () {
+                };
                 $.fn.select2.defaults.set('width', '100%');
                 $.fn.select2.defaults.set('width', '100%');
                 $('#unitSelector').select2({
                 $('#unitSelector').select2({
                     placeholder: '必填,请选择单元',
                     placeholder: '必填,请选择单元',
@@ -53,21 +54,21 @@
                         data: function (params) {
                         data: function (params) {
                             console.log("param", params);
                             console.log("param", params);
                             var _term = "";
                             var _term = "";
-                            if(params.hasOwnProperty("term")){
+                            if (params.hasOwnProperty("term")) {
                                 _term = params.term;
                                 _term = params.term;
                             }
                             }
                             return {
                             return {
                                 unitNum: _term,
                                 unitNum: _term,
                                 page: 1,
                                 page: 1,
-                                row:10,
-                                floorId:vc.component.unitSelect2Info.floorId,
-                                communityId:vc.getCurrentCommunity().communityId
+                                row: 10,
+                                floorId: this.unitSelect2Info.floorId,
+                                communityId: vc.getCurrentCommunity().communityId
                             };
                             };
                         },
                         },
                         processResults: function (data) {
                         processResults: function (data) {
-                            console.log(data, vc.component._filterUnitData(data));
+                            console.log(data, this._filterUnitData(data));
                             return {
                             return {
-                                results: vc.component._filterUnitData(data)
+                                results: this._filterUnitData(data)
                             };
                             };
                         },
                         },
                         cache: true
                         cache: true
@@ -76,24 +77,24 @@
                 $('#unitSelector').on("select2:select", function (evt) {
                 $('#unitSelector').on("select2:select", function (evt) {
                     //这里是选中触发的事件
                     //这里是选中触发的事件
                     //evt.params.data 是选中项的信息
                     //evt.params.data 是选中项的信息
-                    console.log('select',evt);
-                    vc.component.unitSelect2Info.unitId = evt.params.data.id;
-                    vc.component.unitSelect2Info.unitName = evt.params.data.text;
+                    console.log('select', evt);
+                    this.unitSelect2Info.unitId = evt.params.data.id;
+                    this.unitSelect2Info.unitName = evt.params.data.text;
                 });
                 });
 
 
                 $('#unitSelector').on("select2:unselect", function (evt) {
                 $('#unitSelector').on("select2:unselect", function (evt) {
                     //这里是取消选中触发的事件
                     //这里是取消选中触发的事件
                     //如配置allowClear: true后,触发
                     //如配置allowClear: true后,触发
-                    console.log('unselect',evt)
+                    console.log('unselect', evt)
 
 
                 });
                 });
             },
             },
-            _filterUnitData:function (_units) {
+            _filterUnitData: function (_units) {
                 var _tmpUnits = [];
                 var _tmpUnits = [];
                 for (var i = 0; i < _units.length; i++) {
                 for (var i = 0; i < _units.length; i++) {
                     var _tmpUnit = {
                     var _tmpUnit = {
-                        id:_units[i].unitId,
-                        text:_units[i].unitNum
+                        id: _units[i].unitId,
+                        text: _units[i].unitNum
                     };
                     };
                     _tmpUnits.push(_tmpUnit);
                     _tmpUnits.push(_tmpUnit);
                 }
                 }

+ 6 - 1
java110-front/src/main/java/com/java110/front/core/VcCreateProcessor.java

@@ -103,9 +103,14 @@ public class VcCreateProcessor extends AbstractElementTagProcessor {
 
 
         String namespace = tag.getAttributeValue("namespace");
         String namespace = tag.getAttributeValue("namespace");
 
 
-        return js.replace("this.", "vc.component." + namespace + "_")
+        js = js.replace("this.", "vc.component." + namespace + "_")
                 .replaceAll("(\\$)+( )*+(\\()+( )*+\'+#", "\\$('#" + namespace + "_")
                 .replaceAll("(\\$)+( )*+(\\()+( )*+\'+#", "\\$('#" + namespace + "_")
                 .replaceAll("(\\$)+( )*+(\\()+( )*+\"+#", "\\$(\"#" + namespace + "_");
                 .replaceAll("(\\$)+( )*+(\\()+( )*+\"+#", "\\$(\"#" + namespace + "_");
+
+        //将 监听也做优化
+        js = js.replaceAll("(vc.on)+\\('", "vc.on('" + namespace + ",");
+
+        return js;
     }
     }
 
 
 
 

+ 25 - 0
java110-front/src/main/resources/static/js/vc-core.js

@@ -38,6 +38,31 @@
         vc.component.$emit(_componentName + '_' + _value, _param);
         vc.component.$emit(_componentName + '_' + _value, _param);
     };
     };
 
 
+    /**
+     事件监听
+     **/
+    vc.on = function (_namespace, _componentName, _value, _callback) {
+
+        vc.component.$on(_namespace + "_" + _componentName + '_' + _value,
+            function (param) {
+                if (vc.debug) {
+                    console.log("监听ON事件", _namespace, _componentName, _value, param);
+                }
+                _callback(param);
+            }
+        );
+    };
+
+    /**
+     事件触发
+     **/
+    vc.emit = function (_namespace, _componentName, _value, _param) {
+        if (vc.debug) {
+            console.log("监听emit事件", _namespace, _componentName, _value, _param);
+        }
+        vc.component.$emit(_namespace + "_" + _componentName + '_' + _value, _param);
+    };
+
 })(window.vc);
 })(window.vc);
 
 
 /**
 /**