project.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var PROJECT = {
  5. createNew: function (projectID, userID) {
  6. // 定义private方法
  7. var tools = {
  8. _projectID: projectID,
  9. _userID: userID,
  10. updateLock: 0,
  11. updateData: [],
  12. operation: ''
  13. };
  14. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  15. var project = function () {
  16. this.mainTree = cacheTree.createNew(this);
  17. this.Bills = Bills.createNew(this);
  18. this.Rations = Rations.createNew(this);
  19. this.GLJ = GLJs.createNew(this);
  20. this.masterField = {ration: 'BillsID'};
  21. };
  22. // prototype用于定义public方法
  23. project.prototype.modify = function (modifyDatas, callback) {
  24. // To Do
  25. };
  26. project.prototype.loadMainTree = function () {
  27. var that = this;
  28. var loadRationNode = function (rations, cacheNode) {
  29. var newNode;
  30. rations.forEach(function (ration) {
  31. if (ration[that.masterField.ration] && ration[that.masterField.ration] === cacheNode.source.getID()) {
  32. newNode = that.mainTree.addNode(cacheNode);
  33. newNode.source = ration;
  34. newNode.sourceType = that.Rations.getSourceType();
  35. newNode.data = ration;
  36. }
  37. });
  38. };
  39. var loadIdTreeNode = function (nodes, parent) {
  40. var newNode, i;
  41. for (i = 0; i < nodes.length; i++) {
  42. newNode = that.mainTree.addNode(parent);
  43. newNode.source = nodes[i];
  44. newNode.sourceType = that.Bills.getSourceType();
  45. newNode.data = nodes[i].data;
  46. if (nodes[i].children.length === 0) {
  47. loadRationNode(that.Rations.datas, newNode);
  48. } else {
  49. loadIdTreeNode(nodes[i].children, newNode);
  50. }
  51. }
  52. };
  53. loadIdTreeNode(this.Bills.tree.roots, null);
  54. this.mainTree.sortTreeItems();
  55. };
  56. project.prototype.beginUpdate = function(operation){
  57. if (tools.updateLock === 0){
  58. tools.operation = operation
  59. }
  60. tools.updateLock += 1;
  61. };
  62. project.prototype.endUpdate = function(){
  63. if (tools.updateLock === 0){
  64. throw "project can not endUpdate before beginUpdate";
  65. }
  66. tools.updateLock -= 1;
  67. if (tools.updateLock === 0) {
  68. $.ajax({
  69. type: "POST",
  70. url: '/project/save',
  71. data: {'data': JSON.stringify({
  72. "project_id": tools._projectID,
  73. "user_id": tools._userID,
  74. "date": new Date,
  75. "operation": tools.operation,
  76. "update_data": tools.updateData
  77. })},
  78. dataType: 'json',
  79. cache: false,
  80. timeout: 50000,
  81. success: function (result) {
  82. if (result.error === 0) {
  83. // to do callback(result.data);
  84. } else {
  85. alert('error: ' + result.message);
  86. }
  87. },
  88. error: function(jqXHR, textStatus, errorThrown){
  89. alert('error ' + textStatus + " " + errorThrown);
  90. }
  91. });
  92. tools.updateData = [];
  93. tools.operation = "";
  94. }
  95. };
  96. project.prototype.push = function(moduleName, data){
  97. var moduleData = {
  98. moduleName: moduleName,
  99. data: data
  100. };
  101. tools.updateData.push(moduleData);
  102. };
  103. return new project();
  104. }
  105. };