js 对象使用

js对象操作


(function(){
    /**
     *  Author:LiAnAn
     *  WorkflowAPI 使用说明
     *  1:参数后面带Object,一律是对象(例: Activity() )
     *  2:参数后面带Array,一律是数组(例:Attributes() )
     *  3:对象空,填Null
     *  4:数组空,填Empty
     */


    /**
     * Workflow 单实例对象
     * @type {{id: string, createTime: string, sn: string, status: string, extends: {}, code: string, type: string, processLibId: string, creator: string, version: string, modifier: string, category: string, items: {ACTIVITY: Array}, name: string, owner: string, attributes: {}, $id: string, lastModifyTime: string}}
     */
    var Workflow = {
        "id" : "",
        "createTime": "",
        "sn" : "",
        "status" : "",
        "extends" : {},
        "code": "",
        "type" : "",
        "processLibId" : "",
        "creator" : "",
        "version": "",
        "modifier": "",
        "category": "",
        "items" : { "ACTIVITY" : [],"SEQUENCE_FLOW" : [] },
        "name" : "",
        "owner" : "",
        "attributes" : {},
        "$id" : "",
        "lastModifyTime" : ""
    };


    /**
     * new Attributes(basicsArray,permissionArray)
     * @param basicsArray
     * @param permissionArray
     * @constructor
     */
    function Attributes(basicsArray,permissionArray) {
        this.basics = basicsArray;
        this.permission = permissionArray;
    }


    /**
     * new Basics(valueText,name,value,group)
     * @param valueText
     * @param name
     * @param value
     * @param group
     * @constructor
     */
    function Basics(valueText,name,value,group){
        this.valueText = valueText;
        this.name = name;
        this.value = value;
        this.group = group;
    }

    /**
     * new Permission(valueText,name,value,group)
     * @param valueText
     * @param name
     * @param value
     * @param group
     * @constructor
     */
    function Permission(valueText,name,valueArray,group){
        this.valueText = valueText;
        this.name = name;
        this.value = valueArray;
        this.group = group;
    }

    /**
     * new Extends(ref)
     * @param ref
     * @constructor
     */
    function Extends(ref){
        this.ref = ref;
    }


    /**
     * new Activity(id,category,itemsObject,extendsObject,name,attributesObject,$id)
     * @param id
     * @param category
     * @param itemsObject
     * @param extendsObject
     * @param name
     * @param attributesObject
     * @param $id
     * @constructor
     */
    function Activity(id,category,itemsObject,extendsObject,name,attributesObject,$id){
        this.id = id;
        this.category = category;
        this.items = itemsObject;
        this.extends = extendsObject;
        this.name = name;
        this.attributes = attributesObject;
        this.$id = $id;
    }

    /**
     * new SequenceFlow(id,category,itemsObject,extendsObject,targetId,attributesObject,$id,sourceId)
     * @param id
     * @param category
     * @param itemsObject
     * @param extendsObject
     * @param targetId
     * @param attributesObject
     * @param $id
     * @param sourceId
     * @constructor
     */
    function SequenceFlow(id,category,itemsObject,extendsObject,targetId,attributesObject,$id,sourceId){
        this.id = id;
        this.category = category;
        this.items = itemsObject;
        this.extends = extendsObject;
        this.targetId = targetId;
        this.attributes = attributesObject;
        this.$id = $id;
        this.sourceId = sourceId;
    }


    /**
     * 字符串空
     * @type {{}}
     */
    var Null = {

    }

    /**
     * 空数组
     * @type {Array}
     */
    var Empty = new Array();

    /**
     *
     * @param data
     * @returns {{setId: Function, getId: Function, setCreateTime: Function, getCreateTime: Function, setSn: Function,
      * getSn: Function, setStatus: Function, getStatus: Function, getExtendsObject: Function, setExtends: Function,
       * setCode: Function, getCode: Function, setType: Function, getType: Function, setProcessLibId: Function,
       * getProcessLibId: Function, setCreator: Function, getCreator: Function, setVersion: Function,
       * getVersion: Function, setModifier: Function, getModifier: Function, setCategory: Function,
       * getCategory: Function, setItemsActivityArray: Function, setItemsSequenceFlowArray: Function,
       * getItemsActivityArray: Function, getItemsSequenceFlowArray: Function, setName: Function,
       * getName: Function, setOwner: Function, getOwner: Function, setAttributes: Function,
       * getAttributes: Function, set$id: Function, get$id: Function, setLastModifyTime: Function,
       * getLastModifyTime: Function, toJson: Function, toJson: Function, parseJson: Function,
       * addObjectToArray: Function, newArray: Function, post: Function, covertObject: Function}}
     */
    function covertObject(data){
        if(!data[globalConfig.ajaxRetrnErrCode]){

            var json = JSON.stringify(data[globalConfig.resultObjectName].workflowController);
            var result = WorkflowAPI.parseJson(json);
            if(result.hasOwnProperty("createTime")){
                WorkflowAPI.setCreateTime(result.createTime);
            }
            if(result.hasOwnProperty("sn")){
                WorkflowAPI.setSn(result.sn);
            }
            if(result.hasOwnProperty("status")){
                WorkflowAPI.setStatus(result.status)
            }
            if(result.hasOwnProperty("code")){
                WorkflowAPI.setCode(result.code);
            }
            if(result.hasOwnProperty("type")){
                WorkflowAPI.setType(result.type);
            }
            if(result.hasOwnProperty("processLibId")){
                WorkflowAPI.setProcessLibId(result.processLibId);
            }
            if(result.hasOwnProperty("creator")){
                WorkflowAPI.setCreator(result.creator);
            }
            if(result.hasOwnProperty("version")){
                WorkflowAPI.setVersion(result.version);
            }
            if(result.hasOwnProperty("id")){
                WorkflowAPI.setId(result.id);
            }
            if(result.hasOwnProperty("$id")){
                WorkflowAPI.set$id(result.$id);
            }
            if(result.hasOwnProperty("modifier")){
                WorkflowAPI.setModifier(result.modifier);
            }
            if(result.hasOwnProperty("category")){
                WorkflowAPI.setCategory(result.category);
            }
            if(result.hasOwnProperty("name")){
                WorkflowAPI.setName(result.name);
            }
            if(result.hasOwnProperty("owner")){
                WorkflowAPI.setOwner(result.owner);
            }
            if(result.hasOwnProperty("lastModifyTime")){
                WorkflowAPI.setLastModifyTime(result.lastModifyTime);
            }
            if(result.hasOwnProperty("extends")){
                if(result.extends.hasOwnProperty("ref")){
                    WorkflowAPI.setExtends(new Extends(result.extends.ref));
                }
            }
            if(result.hasOwnProperty("attributes")){
                if(result.attributes.hasOwnProperty("basics") || result.attributes.hasOwnProperty("permission")){
                    if(result.attributes.hasOwnProperty("basics")){
                        WorkflowAPI.setAttributes(new Attributes(result.attributes.basics,Empty));
                    }
                    if(result.attributes.hasOwnProperty("permission")){
                        WorkflowAPI.setAttributes(new Attributes(Empty,result.attributes.permission));
                    }
                    if(result.attributes.hasOwnProperty("basics") && result.attributes.hasOwnProperty("permission")){
                        WorkflowAPI.setAttributes(new Attributes(result.attributes.basics,result.attributes.permission));
                    }

                }else{
                    WorkflowAPI.setAttributes(Null);
                }
            }

            if(result.hasOwnProperty("items")){
                if(result.items.hasOwnProperty("ACTIVITY") || result.items.hasOwnProperty("SEQUENCE_FLOW")){
                    if(result.items.hasOwnProperty("ACTIVITY")){
                        WorkflowAPI.setItemsActivityArray(result.items.ACTIVITY);
                    }
                    if(result.items.hasOwnProperty("SEQUENCE_FLOW")){
                        WorkflowAPI.setItemsSequenceFlowArray(result.items.SEQUENCE_FLOW);
                    }
                }
            }
            return WorkflowAPI;
        }
    }


    /**
     *
     * @param id
     * @param callback
     */
    function post(id,callback){
        $.ajax({
            url: globalConfig.dataServer+"/api/workflow/get",
            type: globalConfig.ajaxPostMethod,
            cache: false,
            "async": true,
            data: {
                id : id
            },
            dataType:globalConfig.ajaxDataType,
            success: function(data){
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(globalConfig.messageTexts.ajaxFailure);
                console.log(errorThrown);
            }
        });
    }

    /**
     *
     * @type {{setId: Function, getId: Function, setCreateTime: Function, getCreateTime: Function, setSn: Function,
      * getSn: Function, setStatus: Function, getStatus: Function, getExtendsObject: Function, setExtends: Function,
       * setCode: Function, getCode: Function, setType: Function, getType: Function, setProcessLibId: Function,
       * getProcessLibId: Function, setCreator: Function, getCreator: Function, setVersion: Function,
       * getVersion: Function, setModifier: Function, getModifier: Function, setCategory: Function,
       * getCategory: Function, setItemsActivityArray: Function, setItemsSequenceFlowArray: Function,
       * getItemsActivityArray: Function, getItemsSequenceFlowArray: Function, setName: Function,
       * getName: Function, setOwner: Function, getOwner: Function, setAttributes: Function,
       * getAttributes: Function, set$id: Function, get$id: Function, setLastModifyTime: Function,
       * getLastModifyTime: Function, toJson: Function, toJson: Function, parseJson: Function,
       * addObjectToArray: Function, newArray: Function, post: Function, covertObject: Function}}
     */
    var WorkflowAPI = {
        setId : function(id){
            Workflow.id = id;
        },
        getId : function(){
            return Workflow.id;
        },
        setCreateTime : function(createTime){
            Workflow.createTime = createTime;
        },
        getCreateTime : function(){
            return Workflow.sn;
        },
        setSn : function(sn){
            Workflow.sn = sn;
        },
        getSn : function(){
            return this.createTime;
        },
        setStatus : function(status){
            Workflow.status = status;
        },
        getStatus : function(){
            return Workflow.status;
        },
        getExtendsObject : function(){
            return Extends;
        },
        setExtends : function(extendsObject){
            Workflow.extends = extendsObject;
        },
        setCode : function(code){
            Workflow.code = code;
        },
        getCode : function(){
            return Workflow.code;
        },
        setType : function(type){
            Workflow.type = type;
        },
        getType : function(){
            return Workflow.type;
        },
        setProcessLibId : function(processLibId){
            Workflow.processLibId = processLibId;
        },
        getProcessLibId : function(){
            return Workflow.processLibId;
        },
        setCreator : function(creator){
            Workflow.creator = creator;
        },
        getCreator : function(){
            return Workflow.creator;
        },
        setVersion : function(version){
            Workflow.version = version;
        },
        getVersion : function(){
            return Workflow.version;
        },
        setModifier : function(modifier){
            Workflow.modifier = modifier;
        },
        getModifier : function(){
            return Workflow.modifier;
        },
        setCategory : function(category){
            Workflow.category = category;
        },
        getCategory : function(){
            return Workflow.category;
        },
        setItemsActivityArray : function(activityArray){
            Workflow.items.ACTIVITY = activityArray;
        },
        setItemsSequenceFlowArray : function(sequenceFlowArray){
            Workflow.items.SEQUENCE_FLOW = sequenceFlowArray;
        },
        getItemsActivityArray : function(){
            return Workflow.items.ACTIVITY;
        },
        getItemsSequenceFlowArray : function(){
            return Workflow.items.SEQUENCE_FLOW;
        },
        setName : function(name){
            Workflow.name = name;
        },
        getName : function(){
            return Workflow.name;
        },
        setOwner : function(owner){
            Workflow.owner = owner;
        },
        getOwner : function(){
            return Workflow.owner;
        },
        setAttributes : function(attributesObject){
            Workflow.attributes = attributesObject;
        },
        getAttributes : function(){
            return Workflow.attributes;
        },
        set$id : function($id){
            Workflow.$id = $id;
        },
        get$id : function(){
            return Workflow.$id;
        },
        setLastModifyTime : function(lastModifyTime){
            Workflow.lastModifyTime = lastModifyTime;
        },
        getLastModifyTime : function(){
            return Workflow.lastModifyTime;
        },
        toJson : function(obj){
            return JSON.stringify(obj);
        },
        toJson : function(){
            return JSON.stringify(Workflow).replace("SEQUENCE_FLOW","SEQUENCE-FLOW");
        },
        parseJson : function(json){
            json = json.replace("SEQUENCE-FLOW","SEQUENCE_FLOW");
            return JSON.parse(json);
        },
        addObjectToArray : function(array,object){
            array[array.length] = object;
        },
        newArray : function(){
            return new Array();
        },
        post : function(id,callback){
            return post(id,callback);

        },
        covertObject : function(data){
            WorkflowAPI = covertObject(data);
            return WorkflowAPI;
        }



};




    window['WorkflowAPI'] =  WorkflowAPI;
    window['Extends'] =  Extends;
    window['Attributes'] =  Attributes;
    window['Basics'] =  Basics;
    window['Permission'] =  Permission;
    window['Activity'] =  Activity;
    window['SequenceFlow'] =  SequenceFlow;
    window['Null'] =  Null;
    window['Empty'] =  Empty;
})();



Test demo

<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="./../../../jquery/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="workflowAPI.js" type="text/javascript"></script>
</head>
<body>
<h1>测试js中......</h1>
<script type="text/javascript">
    (function(){

        var w = WorkflowAPI;

        w.setId("1");
        w.setCreateTime("20123-12-123");
        w.setSn("3");
        w.setStatus("4");
        w.setExtends(new Extends("16"));
        w.setType("6");
        w.setProcessLibId(7);
        w.setCreator(8);
        w.setVersion(9);
        w.setModifier(10);
        w.setCategory(11);
        w.setCode(12);
        w.setName(13);

        var array = w.newArray();
        var array1 = w.newArray();
        w.addObjectToArray(array,new Basics("111","111","111","111"));
        w.addObjectToArray(array,new Basics("222","222","222","222"));
        w.addObjectToArray(array1,new Permission("333","333","333","333"))
        w.setAttributes(new Attributes(array,Empty));
        var array2 = w.newArray();
        w.addObjectToArray(array2,new Activity("id","category",Null,new Extends("123"),"name",new Attributes(array,array1),"$id"));
        w.addObjectToArray(array2,new Activity("id","category",Null,new Extends("123"),"name",new Attributes(array,array1),"$id"));
        w.setItemsActivityArray(array2);
        w.set$id(14);
        w.setLastModifyTime("2-12312");
        w.setOwner("15");

        alert(w.toJson());
    })();
</script>
</body>
</html>



猜你喜欢

转载自blog.csdn.net/an74520/article/details/49588487