this.Dict是一个工具类,如果您在流程、内容管理、门户和服务管理中创建了数据字典,可以使用this.Dict类对数据字典进行增删改查操作。
从v8.0版本开始,支持在门户和服务管理中创建数据字典。
Syntax
//您可以在页面、表单、流程各个嵌入脚本中,通过this.Dict()对本应用或其他应用的数据字典中的数据进行增删改查,如下:
var dict = new this.Dict( options )Parameters
- 
            
                optionsOrName
            
            
                
    
        String|Object数据字典标识字符串或者是对象。 如果对本应用的数据字典操作,将optionsOrName设置为string。var dict = new this.Dict("bulletinDictionary"); //数据字典的名称、别名或id如果需要对其他应用的数据字典进行操作,将options设置为JsonObjectvar dict = new this.Dict({ //type: 应用类型。可以为process cms portal service。 //在流程和内容管理中如果没有该选项或者值为空字符串,则表示应用脚本和被应用的脚本配置类型相同。 //比如在流程的A应用脚本中引用流程B应用的脚本配置,则type可以省略。 //为了兼容老版本,在门户中使用需要指定type,否则默认值为process type : "cms", application : "bulletin", //数据字典所在的流程、门户、CMS的名称、别名、id, 默认为当前应用,服务管理中忽略该参数 name : "bulletinDictionary", // 数据字典的名称、别名、id anonymous : true //允许用户在未登录的情况下读取cms的数据字典, type为cms的时候改参数才有效,默认为false,该参数名也可以是 enableAnonymous });//引用服务管理中的数据字典 var dict = new this.Dict({ "type": "service", "name": "dictName" }); //引用流程管理中的数据字典 var dict = new this.Dict({ "type": "process", "application": "appName", "name": "dictName" }); //引用内容管理中的数据字典 var dict = new this.Dict({ "type": "cms", "application": "appName", "name": "dictName" }); //引用门户管理中的数据字典 var dict = new this.Dict({ "type": "portal", "application": "appName", "name": "dictName" }); 
Returns
- 
            
                ObjectDict对象 
Source
Methods
get(pathopt, successopt, failureopt, asyncopt, refreshopt) → {Promise|Object|Array|String|Number|Boolean}
根据路径获取数据字典中的数据。
Syntax
var data = dict.get( path, success, failure, async, refresh )Parameters
- 
            
                path
            
            
                
    
        String<optional>
 数据字典中的数据路径,允许使用中文。当路径为多级时,用点号(.)分隔。当值为空的时候,表示获取数据字典中的所有数据。 
- 
            
                success
            
            
                
    
        function<optional>
 获取数据成功时的回调函数。 
- 
            
                failure
            
            
                
    
        function<optional>
 获取数据失败时的回调。 
- 
            
                async
            
            
                
    
        Boolean<optional>
 是否异步执行,默认为false。 
- 
            
                refresh
            
            
                
    
        Boolean<optional>
 是否忽略本地缓存直接从服务器获取,默认为false。如果需要在脚本中该数据字典是变化的(比如编号),需设置为true。 
Returns
- 
            
                PromiseObjectArrayStringNumberBoolean当async为true时返回Promise(Promise说明); 否则返回数据字典的数据,类型和配置数据字典时候指定的一致。 
Examples
var dict = new this.Dict("bulletinDictionary");
//没有参数的时候,表示同步获取获取所有数据
 var data = dict.get()
 //同步执行,获取category下key为subCategory的数据
 var data = dict.get("category.subCategory");
//异步执行,使用回调处理数据,如果category为数组,获取第0项数据
dict.get("category.0", function(data){
     //data 是数据字典的数据
 }, function(xhr){
     //xhr 为 xmlHttpRequest
 }, true //异步执行
)
//异步执行,使用Promise处理结果
var promise = dict.get("category", null, null, true);
promise.then( function(data){
    //data 是数据字典的数据
})
    已经配置好了如下图所示的数据字典
 
var dict = new this.Dict({
    //type: 应用类型。可以为process  cms。
    //如果没有该选项或者值为空字符串,则表示应用脚本和被应用的脚本配置类型相同。
    //比如在流程的A应用脚本中引用流程B应用的脚本配置,则type可以省略。
   type : "cms",
   application : "bulletin", //流程、CMS的名称、别名、id, 默认为当前应用
   name : "bulletinDictionary", // 数据字典的名称、别名、id
   anonymous : true //允许用户在未登录的情况下读取cms的数据字典, type为process的时候此参数无效,默认为false,该参数名也可以是enableAnonymous
});
var data = dict.get();
//data的值为
{
   "category": [
       {
           "enable": true,
           "sequence": 1.0,
           "text": "公司公告",
           "value": "company"
       },
       {
           "enable": "false",
           "sequence": 2.0,
           "text": "部门公告",
           "value": "department"
       }
   ]
}
 var category = dict.get("category");
 //category的值为
 [
    {
       "enable": true,
       "sequence": 1.0,
       "text": "公司公告",
       "value": "company"
   },
    {
      "enable": "false",
      "sequence": 2.0,
       "text": "部门公告",
       "value": "department"
   }
 ]
 var array0 = dict.get("category.0");
 //array0 的值为
 {
   "enable": true,
   "sequence": 1.0,
   "text": "公司公告",
   "value": "company"
}
var enable = dict.get("category.0.eanble");
//enable 的值为 trueSource
add(path, data, successopt, failureopt)
根据路径新增数据字典的数据。
Syntax
dict.add( path, data, success, failure )Parameters
- 
            
                path
            
            
                
    
        String数据字典中的数据路径,允许使用中文。当路径为多级时,用点号(.)分隔。如果path在数据字典中已有数据,且原有数据是数组,则数组添加一项;如果原有数据不是数组,则报错。 
- 
            
                data
            
            
                
    
        Object|Array|String|Number|Boolean需要新增的数据 
- 
            
                success
            
            
                
    
        function<optional>
 增加数据成功时的回调函数。 
- 
            
                failure
            
            
                
    
        function<optional>
 增加数据错误时的回调函数。 
Examples
var dict = new this.Dict("bulletinDictionary");
dict.add( "category", { text : "系统公告", value : "system" }, function(data){
   //data 形如
   //{
   //    "id": "80ed5f60-500f-4358-8bbc-b7e81f77aa39" //id为数据字典ID
   //}
}, function(xhr){
   //xhr 为 xmlHttpRequest
});对get方法样例的数据字典进行赋值,如下:
var dict = new this.Dict("bulletinDictionary");
dict.add( "category", { text : "系统公告", value : "system" }, function(data){
   //data 形如
   //{
   //    "id": "80ed5f60-500f-4358-8bbc-b7e81f77aa39" //id为数据字典ID
   //}
}, function(xhr, text, error){
   //xhr 为 xmlHttpRequest, text 为错误文本, error为Error对象
});
    //数据字典的值变为
{
   "category": [
       {
           "enable": true,
           "sequence": 1.0,
           "text": "公司公告",
           "value": "company"
       },
       {
           "enable": "false",
           "sequence": 2.0,
           "text": "部门公告",
           "value": "department"
       },
       {
           "text": "系统公告",
           "value": "system"
       }
   ]
}
 dict.add( "category.2.sequence", 3 );
    //数据字典的值变为
{
   "category": [
       {
           "enable": true,
           "sequence": 1.0,
           "text": "公司公告",
           "value": "company"
       },
       {
           "enable": "false",
           "sequence": 2.0,
           "text": "部门公告",
           "value": "department"
       },
       {
           "sequence" : 3.0,
           "text": "系统公告",
           "value": "system"
       }
   ]
}
dict.add( "archiveOptions", {
   "yes" : "是",
   "no" : "否"
});
    //数据字典的值变为
{
   "category": [
       {
           "enable": true,
           "sequence": 1.0,
           "text": "公司公告",
           "value": "company"
       },
       {
           "enable": "false",
           "sequence": 2.0,
           "text": "部门公告",
           "value": "department"
       },
       {
           "sequence" : 3.0,
           "text": "系统公告",
           "value": "system"
       }
   ],
   "archiveOptions" : {
       "yes" : "是",
       "no" : "否"
   }
}下面是错误的赋值,如下:
dict.add( "category.3", { text : "系统公告", value : "system" }); //出错,因为不能对数组下标直接赋值
dict.add( "category.1.value", { text : "系统公告" } ); //出错,因为不能对已经存在的非数组路径赋值Source
set(path, data, successopt, failureopt)
根据路径修改数据字典的数据。
Syntax
dict.set( path, data, success, failure )Parameters
- 
            
                path
            
            
                
    
        String数据字典中的数据路径,允许使用中文。当路径为多级时,用点号(.)分隔。如果数据路径不存在,则报错。 
- 
            
                data
            
            
                
    
        Object|Array|String|Number|Boolean修改后的数据 
- 
            
                success
            
            
                
    
        function<optional>
 设置数据成功时的回调函数。 
- 
            
                failure
            
            
                
    
        function<optional>
 设置数据错误时的回调函数。 
Examples
var dict = new this.Dict("bulletinDictionary");
dict.set( "category", { text : "系统公告", value : "system" }, function(data){
   //data 形如
   //{
   //    "id": "80ed5f60-500f-4358-8bbc-b7e81f77aa39" //id为数据字典ID
   //}
}, function(xhr){
   //xhr 为 xmlHttpRequest
});对Example add的数据字典进行赋值,如下:
var dict = new this.Dict("bulletinDictionary");
dict.set( "archiveOptions", [ { text : "是" }, { text : "否" } ]);
     //数据字典的值变为
 {
    "category": [
        {
            "enable": true,
            "sequence": 1.0,
            "text": "公司公告",
            "value": "company"
        },
        {
            "enable": "false",
            "sequence": 2.0,
            "text": "部门公告",
            "value": "department"
        },
        {
            "sequence" : 3.0,
            "text": "系统公告",
            "value": "system"
        }
    ],
    "archiveOptions" : [ { text : "是" }, { text : "否" } ]
}
dict.set( "category.2", { text : "县级公告", value : "county" }, function(data){
    //data 形如
    //{
    //    "id": "80ed5f60-500f-4358-8bbc-b7e81f77aa39" //id为数据字典ID
    //}
 }, function(xhr){
    //xhr 为 xmlHttpRequest
 });
  /数据字典的值变为
 {
    "category": [
        {
            "enable": true,
            "sequence": 1.0,
            "text": "公司公告",
            "value": "company"
        },
        {
            "enable": "false",
            "sequence": 2.0,
            "text": "部门公告",
            "value": "department"
        },
        {
            "text": "县级公告",
            "value": "county"
        }
    ],
    "archiveOptions" : [ { text : "是" }, { text : "否" } ]
}
dict.set( "category.1.sequence", 3 );
dict.set( "category.2.sequence", 2 );
     //数据字典的值变为
     {
    "category": [
        {
            "enable": true,
            "sequence": 1.0,
            "text": "公司公告",
            "value": "company"
        },
        {
            "enable": "false",
            "sequence": 3.0,
            "text": "部门公告",
            "value": "department"
        },
        {
            "sequence": 2.0,
            "text": "县级公告",
            "value": "county"
        }
    ],
    "archiveOptions" : [ { text : "是" }, { text : "否" } ]
}下面是错误的赋值:
dict.set( "category_1", { text : "公司公告" } ); //出错,因为category_1在数据字典中不存在Source
delete(path, successopt, failureopt)
根据路径删除数据字典的数据。
Syntax
dict.delete( path, success, failure )Parameters
- 
            
                path
            
            
                
    
        String数据字典中的数据路径,允许使用中文。当路径为多级时,用点号(.)分隔。如果数据路径不存在,则报错。如果删除数组中的某一项,只能删除最后一项。 
- 
            
                success
            
            
                
    
        function<optional>
 删除数据成功时的回调函数。 
- 
            
                failure
            
            
                
    
        function<optional>
 删除数据错误时的回调函数。 
Examples
var dict = new this.Dict("bulletinDictionary");
dict.delete( "category", function(){
}, function(xhr){
   //xhr 为 xmlHttpRequest
});对Example set的数据字典进行赋值,如下:
var dict = new this.Dict("bulletinDictionary");
dict.delete( "archiveOptions");
//数据字典的值变为
{
   "category": [
       {
           "enable": true,
           "sequence": 1.0,
           "text": "公司公告",
    *            "value": "company"
       },
       {
           "enable": "false",
           "sequence": 3.0,
           "text": "部门公告",
           "value": "department"
       },
       {
           "sequence": 2.0,
           "text": "县级公告",
           "value": "county"
       }
   ]
}
dict.delete( "category.2.sequence", function(data){
   //data 形如
   //{
   //    "id": "80ed5f60-500f-4358-8bbc-b7e81f77aa39" //id为数据字典ID
   //}
}, function(xhr){
   //xhr 为 xmlHttpRequest
});
//数据字典的值变为
{
   "category": [
       {
           "enable": true,
           "sequence": 1.0,
           "text": "公司公告",
           "value": "company"
       },
       {
           "enable": "false",
           "sequence": 3.0,
           "text": "部门公告",
           "value": "department"
       },
       {
           "text": "县级公告",
           "value": "county"
       }
   ]
}
dict.delete( "category.2");
//category是数组,只能删除最后一项也就是下标2
//数据字典的值变为
{
   "category": [
       {
           "enable": true,
           "sequence": 1.0,
           "text": "公司公告",
           "value": "company"
       },
       {
           "enable": "false",
           "sequence": 3.0,
           "text": "部门公告",
           "value": "department"
       }
   ]
}下面是错误的删除:
dict.delete( "category_1" ); //出错,因为category_1在数据字典中不存在