project.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var PROJECT = {
  5. <<<<<<< HEAD
  6. createNew: function () {
  7. // ����private����
  8. var tools = {};
  9. // ����ͨ��this���ʵ����ԣ�����Ӧ�ڴ˵�Ԫ�ⲿ����д�����
  10. =======
  11. createNew: function (projectID, userID) {
  12. // 定义private方法
  13. var tools = {
  14. _ID: projectID,
  15. _userID: userID,
  16. updateLock: 0,
  17. updateData: [],
  18. operation: '',
  19. modules: {},
  20. doAfterUpdate: function(result){
  21. result.forEach(function(item){
  22. if (item.moduleName in this.modules){
  23. this.modules[item.moduleName].doAfterUpdate(item.err, item.data);
  24. }
  25. });
  26. }
  27. };
  28. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  29. >>>>>>> bc0f5d2858a5b09273a8d6ed5620a4deb05293b2
  30. var project = function () {
  31. this.mainTree = cacheTree.createInit(this);
  32. <<<<<<< HEAD
  33. this.Bills = Bills.createInit(this);
  34. this.Rations = Rations.createInit(this);
  35. this.GLJs = GLJs.createInit(this);
  36. =======
  37. this.Bills = Bills.createNew(this);
  38. this.Ration = Ration.createNew(this);
  39. this.GLJ = GLJ.createNew(this);
  40. >>>>>>> bc0f5d2858a5b09273a8d6ed5620a4deb05293b2
  41. this.masterField = {ration: 'BillsID'};
  42. };
  43. <<<<<<< HEAD
  44. // prototype���ڶ���public����
  45. =======
  46. // prototype用于定义public方法
  47. >>>>>>> bc0f5d2858a5b09273a8d6ed5620a4deb05293b2
  48. project.prototype.modify = function (modifyDatas, callback) {
  49. // To Do
  50. };
  51. // prototype用于定义public方法
  52. project.prototype.ID = function () {
  53. return tools._ID;
  54. };
  55. project.prototype.loadMainTree = function () {
  56. var that = this;
  57. var loadRationNode = function (rations, cacheNode) {
  58. var newNode;
  59. rations.forEach(function (ration) {
  60. if (ration[that.masterField.ration] && ration[that.masterField.ration] === cacheNode.source.getID()) {
  61. newNode = that.mainTree.addNode(cacheNode);
  62. newNode.source = ration;
  63. newNode.sourceType = that.Ration.getSourceType();
  64. newNode.data = ration;
  65. }
  66. });
  67. };
  68. var loadIdTreeNode = function (nodes, parent) {
  69. var newNode, i;
  70. for (i = 0; i < nodes.length; i++) {
  71. newNode = that.mainTree.addNode(parent);
  72. newNode.source = nodes[i];
  73. newNode.sourceType = that.Bills.getSourceType();
  74. newNode.data = nodes[i].data;
  75. if (nodes[i].children.length === 0) {
  76. loadRationNode(that.Ration.datas, newNode);
  77. } else {
  78. loadIdTreeNode(nodes[i].children, newNode);
  79. }
  80. }
  81. };
  82. loadIdTreeNode(this.Bills.tree.roots, null);
  83. this.mainTree.sortTreeItems();
  84. };
  85. // 提供给各模块调用的统一从后台获取数据的方法
  86. project.prototype.pullData = function (url, data, successCallback, errorCallback) {
  87. $.ajax({
  88. type:"POST",
  89. url: url,
  90. data: {'data': JSON.stringify(data)},
  91. dataType: 'json',
  92. cache: false,
  93. timeout: 50000,
  94. success: function(result){
  95. successCallback(result);
  96. },
  97. error: function(jqXHR, textStatus, errorThrown){
  98. alert('error ' + textStatus + " " + errorThrown);
  99. errorCallback();
  100. }
  101. });
  102. };
  103. // 所有模块在此从后台获取数据
  104. project.prototype.loadDatas = function (){
  105. this.Bills.pullData();
  106. this.Ration.pullData();
  107. this.GLJ.pullData();
  108. };
  109. project.prototype.beginUpdate = function(operation){
  110. if (tools.updateLock === 0){
  111. tools.operation = operation
  112. }
  113. tools.updateLock += 1;
  114. };
  115. project.prototype.endUpdate = function(){
  116. if (tools.updateLock === 0){
  117. throw "project can not endUpdate before beginUpdate";
  118. }
  119. tools.updateLock -= 1;
  120. if (tools.updateLock === 0) {
  121. $.ajax({
  122. type: "POST",
  123. url: '/project/save',
  124. data: {'data': JSON.stringify({
  125. "project_id": tools._ID,
  126. "user_id": tools._userID,
  127. "date": new Date,
  128. "operation": tools.operation,
  129. "update_data": tools.updateData
  130. })},
  131. dataType: 'json',
  132. cache: false,
  133. timeout: 50000,
  134. success: function (result) {
  135. if (result.error === 0) {
  136. tools.doAfterUpdate(result.data);
  137. } else {
  138. alert('error: ' + result.message);
  139. }
  140. },
  141. error: function(jqXHR, textStatus, errorThrown){
  142. alert('error ' + textStatus + " " + errorThrown);
  143. }
  144. });
  145. tools.updateData = [];
  146. tools.operation = "";
  147. }
  148. };
  149. project.prototype.push = function(moduleName, data){
  150. if (tools.updateLock === 0){
  151. throw "project can not push data before beginUpdate";
  152. };
  153. var moduleData = {
  154. moduleName: moduleName,
  155. data: data
  156. };
  157. tools.updateData.push(moduleData);
  158. };
  159. project.prototype.registerModule = function(moduleName, obj){
  160. if (!tools.modules.hasOwnProperty(moduleName)){
  161. tools.modules[moduleName] = obj;
  162. }
  163. };
  164. return new project();
  165. }
  166. };