| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- /**
- * Created by Mai on 2017/4/1.
- */
- var PROJECT = {
- createNew: function (projectID, userID) {
- // 定义private方法
- var tools = {
- _projectID: projectID,
- _userID: userID,
- updateLock: 0,
- updateData: [],
- operation: ''
- };
- // 所有通过this访问的属性,都不应在此单元外部进行写入操作
- var project = function () {
- this.mainTree = cacheTree.createNew(this);
- this.Bills = Bills.createNew(this);
- this.Rations = Rations.createNew(this);
- this.GLJ = GLJs.createNew(this);
- this.masterField = {ration: 'BillsID'};
- };
- // prototype用于定义public方法
- project.prototype.modify = function (modifyDatas, callback) {
- // To Do
- };
- project.prototype.loadMainTree = function () {
- var that = this;
- var loadRationNode = function (rations, cacheNode) {
- var newNode;
- rations.forEach(function (ration) {
- if (ration[that.masterField.ration] && ration[that.masterField.ration] === cacheNode.source.getID()) {
- newNode = that.mainTree.addNode(cacheNode);
- newNode.source = ration;
- newNode.sourceType = that.Rations.getSourceType();
- newNode.data = ration;
- }
- });
- };
- var loadIdTreeNode = function (nodes, parent) {
- var newNode, i;
- for (i = 0; i < nodes.length; i++) {
- newNode = that.mainTree.addNode(parent);
- newNode.source = nodes[i];
- newNode.sourceType = that.Bills.getSourceType();
- newNode.data = nodes[i].data;
- if (nodes[i].children.length === 0) {
- loadRationNode(that.Rations.datas, newNode);
- } else {
- loadIdTreeNode(nodes[i].children, newNode);
- }
- }
- };
- loadIdTreeNode(this.Bills.tree.roots, null);
- this.mainTree.sortTreeItems();
- };
- project.prototype.beginUpdate = function(operation){
- if (tools.updateLock === 0){
- tools.operation = operation
- }
- tools.updateLock += 1;
- };
- project.prototype.endUpdate = function(){
- if (tools.updateLock === 0){
- throw "project can not endUpdate before beginUpdate";
- }
- tools.updateLock -= 1;
- if (tools.updateLock === 0) {
- $.ajax({
- type: "POST",
- url: '/project/save',
- data: {'data': JSON.stringify({
- "project_id": tools._projectID,
- "user_id": tools._userID,
- "date": new Date,
- "operation": tools.operation,
- "update_data": tools.updateData
- })},
- dataType: 'json',
- cache: false,
- timeout: 50000,
- success: function (result) {
- if (result.error === 0) {
- // to do callback(result.data);
- } else {
- alert('error: ' + result.message);
- }
- },
- error: function(jqXHR, textStatus, errorThrown){
- alert('error ' + textStatus + " " + errorThrown);
- }
- });
- tools.updateData = [];
- tools.operation = "";
- }
- };
- project.prototype.push = function(moduleName, data){
- var moduleData = {
- moduleName: moduleName,
- data: data
- };
- tools.updateData.push(moduleData);
- };
- return new project();
- }
- };
|