core.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. 初始化处理 vue component
  3. **/
  4. (function(window, undefined){
  5. "use strict";
  6. var vc = window.vc || {};
  7. var _vmOptions = {};
  8. var _initMethod = [];
  9. var _initEvent = [];
  10. var _component = {};
  11. var _destroyedMethod = [];
  12. var _timers = [];//定时器
  13. _vmOptions = {
  14. el:'#component',
  15. data:{
  16. },
  17. watch: {
  18. },
  19. methods:{
  20. },
  21. destroyed:function(){
  22. window.vc.destroyedMethod.forEach(function(eventMethod){
  23. eventMethod();
  24. });
  25. //清理所有定时器
  26. window.vc.timers.forEach(function(timer){
  27. clearInterval(timer);
  28. });
  29. _timers = [];
  30. }
  31. };
  32. vc = {
  33. version:"v0.0.1",
  34. name:"vue component",
  35. author:'java110',
  36. vmOptions:_vmOptions,
  37. initMethod:_initMethod,
  38. initEvent:_initEvent,
  39. component:_component,
  40. destroyedMethod:_destroyedMethod,
  41. timers:_timers
  42. };
  43. //通知window对象
  44. window.vc = vc;
  45. })(window);
  46. /**
  47. vc 函数初始化
  48. add by wuxw
  49. **/
  50. (function(vc){
  51. vc.http = {
  52. post:function(componentCode,componentMethod,param,options,successCallback,errorCallback){
  53. Vue.http.post('/callComponent/'+componentCode +"/"+componentMethod, param, options)
  54. .then(function(res){
  55. successCallback(res.bodyText,res);
  56. }, function(error){
  57. errorCallback(error.bodyText,error);
  58. });
  59. },
  60. get:function(componentCode,componentMethod,param,successCallback,errorCallback){
  61. Vue.http.get('/callComponent/'+componentCode +"/"+componentMethod, param)
  62. .then(function(res){
  63. successCallback(res.bodyText,res);
  64. }, function(error){
  65. errorCallback(error.bodyText,error);
  66. });
  67. }
  68. };
  69. var vmOptions = vc.vmOptions;
  70. //继承方法,合并 _vmOptions 的数据到 vmOptions中
  71. vc.extends = function(_vmOptions){
  72. if(typeof _vmOptions !== "object"){
  73. throw "_vmOptions is not Object";
  74. }
  75. //处理 data 对象
  76. if(_vmOptions.hasOwnProperty('data')){
  77. for(var dataAttr in _vmOptions.data){
  78. vmOptions.data[dataAttr] = _vmOptions.data[dataAttr];
  79. }
  80. }
  81. //处理methods 对象
  82. if(_vmOptions.hasOwnProperty('methods')){
  83. for(var methodAttr in _vmOptions.methods){
  84. vmOptions.methods[methodAttr] = _vmOptions.methods[methodAttr];
  85. }
  86. }
  87. //处理methods 对象
  88. if(_vmOptions.hasOwnProperty('watch')){
  89. for(var watchAttr in _vmOptions.watch){
  90. vmOptions.watch[watchAttr] = _vmOptions.watch[watchAttr];
  91. }
  92. }
  93. //处理_initMethod 初始化执行函数
  94. if(_vmOptions.hasOwnProperty('_initMethod')){
  95. vc.initMethod.push(_vmOptions._initMethod);
  96. }
  97. //处理_initEvent
  98. if(_vmOptions.hasOwnProperty('_initEvent')){
  99. vc.initEvent.push(_vmOptions._initEvent);
  100. }
  101. //处理_initEvent_destroyedMethod
  102. if(_vmOptions.hasOwnProperty('_destroyedMethod')){
  103. vc.destroyedMethod.push(_vmOptions._destroyedMethod);
  104. }
  105. };
  106. //绑定跳转函数
  107. vc.jumpToPage = function(url){
  108. window.location.href = url;
  109. };
  110. })(window.vc);
  111. /**
  112. vc 定时器处理
  113. **/
  114. (function(w,vc){
  115. /**
  116. 创建定时器
  117. **/
  118. vc.createTimer = function(func,sec){
  119. var _timer = w.setInterval(func,sec);
  120. vc.timers.push(_timer); //这里将所有的定时器保存起来,页面退出时清理
  121. return _timer;
  122. };
  123. //清理定时器
  124. vc.clearTimer = function(timer){
  125. clearInterval(timer);
  126. }
  127. })(window,window.vc);