以下的这段代码可用做 CRM 2011 的一个web resource。
其中有几点值得注意:
1.当然本身这个库复用性很高,而且很好用。可以看看它里边定义的方法。
2. 实现 Javascript 命名空间的方法。实际上它只是定义了一个字典型的数组。
if (typeof (GenericLibrary) == "undefined") {
GenericLibrary = { __namespace: true };
}
GenericLibrary = {
Name: "GenericLibrary",
DisplayName: "Generic",
GetServerUrl: function () { return _Generic_GetServerUrl_Implementation(); },
GetODataPath: function () {
return _Generic_GetServerUrl_Implementation() + "/XRMServices/2011/OrganizationData.svc/";
},
SetAttributeRequired: function (filedname, val) {
return _Generic_SetAttributeRequired_Implementation(filedname, val);
},
SetAttributeVisibility: function (filedname, val) {
return _Generic_SetAttributeVisibility_Implementation(filedname, val);
},
SetAttributeDisabled: function (filedname, val) {
return _Generic_SetAttributeDisabled_Implementation(filedname, val);
},
SetAttributeSubmitMode: function (filedname, val) {
return _Generic_SetAttributeSubmitMode_Implementation(filedname, val);
}
};
function _Generic_GetServerUrl_Implementation() {
var serverUrl = Xrm.Page.context.getServerUrl();
if (serverUrl.match(/\/$/)) {
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
}
return serverUrl;
}
function _Generic_SetAttributeVisibility_Implementation(filedname, val) {
var functionName = "Set attribute visibility";
try {
if (Xrm.Page.ui.controls.get(filedname)) {
Xrm.Page.ui.controls.get(filedname).setVisible(val);
}
else {
alert(GenericLibrary.Name + "\n<" + filedname + "> NOT FOUND");
}
}
catch (e) {
alert(GenericLibrary.DisplayName + "\n" + functionName + "\n" + e.message);
return false;
}
}
function _Generic_SetAttributeDisabled_Implementation(filedname, val) {
var functionName = "Set attribute disabled";
try {
if (Xrm.Page.ui.controls.get(filedname)) {
Xrm.Page.ui.controls.get(filedname).setDisabled(val);
}
else {
alert(GenericLibrary.Name + "\n<" + filedname + "> NOT FOUND");
}
}
catch (e) {
alert(GenericLibrary.DisplayName + "\n" + functionName + "\n" + e.message);
return false;
}
}
function _Generic_SetAttributeSubmitMode_Implementation(filedname, value) {
var functionName = "Set attribute submit mode";
try {
if (Xrm.Page.getAttribute(filedname)) {
Xrm.Page.getAttribute(filedname).setSubmitMode(value);
}
else {
alert(GenericLibrary.Name + "\n<" + filedname + "> NOT FOUND");
}
}
catch (e) {
alert(GenericLibrary.DisplayName + "\n" + functionName + "\n" + e.message);
return false;
}
}
function _Generic_SetAttributeRequired_Implementation(filedname, value) {
var functionName = "Set attribute submit mode";
try {
if (Xrm.Page.getAttribute(filedname)) {
if (value)
Xrm.Page.getAttribute(filedname).setRequiredLevel("required");
else
Xrm.Page.getAttribute(filedname).setRequiredLevel("none");
}
else {
alert(GenericLibrary.Name + "\n<" + filedname + "> NOT FOUND");
}
}
catch (e) {
alert(GenericLibrary.DisplayName + "\n" + functionName + "\n" + e.message);
return false;
}
}
欢迎大家关注我的微信公众号阅读更多文章,获取更多知识