project.js 5.7 KB

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