AXForum  
Вернуться   AXForum > Microsoft Dynamics CRM > Dynamics CRM: Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 28.06.2009, 10:06   #1  
Blog bot is offline
Blog bot
Участник
 
25,644 / 848 (80) +++++++
Регистрация: 28.10.2006
mscrm4ever: CRM 4.0 Working with Strings
Источник: http://mscrm4ever.blogspot.com/2009/...h-strings.html
==============



Many CRM development tasks involve manipulation and concatenation of strings, whether it’s concatenating a Field DavaValue from 2 or more fields, building messages that involve dynamic data, constructing FetchXml Requests and Soap messages or constructing strings that contain CSS or other meaningful html information, in one way or another you end up writing a script that looks like the following examples:



//Exampe 1
var someValue = “The account number: ” + crmForm.accountnumber.DataValue + “ is not valid\n”;
someValue+= “More helpful info on how to fill a valid account number here.”

alert(someValue);

or

//Example 2
var iLeft = 100;
var iTop = 100;
var cssText = “position:absolute;left:” + iLeft + “;top:” + iTop + “;”;
document.all[“”].style.cssText = cssText;

or

//Example 3
var iTop = 50;
var iLeft = 50;
var iWidth = 800;
var iHeight = 600;
var windowFeature = “toolbars=0;width=” + iWidth + “,height=” + iHeight + “,top=” + iTop + “,left=” + iLeft;
window.open( “” , “” , windowFeatures);

There are several disadvantages of using this simple concatenation technique:
1.The first which is the most obvious and discussed here is the lack of multi-lingual support for text messages. If for instance you need to translate the text that appears in the first example above, how can you accomplish the task and still keep the dynamic value concatenation in the correct context?


2. Another major disadvantage involves construction of large strings such as when you build soap message or a large FetchXml queries which can dramatically decrease IE responsiveness and cause performance issues.


3. Large string that are constructed in this manner sometimes make it impossible to read and debug


The following JavaScript objects facilitates these types of tasks and will help you avoid the problems mentioned above.


The first code snippet facilitates the concatenation of strings using a class called StringBuilder (similar to C# System.Text.StringBuilder mechanism)

/* JS String Builder */
StringBuilder = function()
{
var parts = [];
this.Append = function( text ){parts[ parts.length ] = text;return this;}
this.Reset = function(){parts = [];}
this.ToString = function(){return parts.join( "" );}
}

//Usage
function OnCrmPageLoad()
{
//Solving example 2
var iLeft = 100;
var iTop = 100;
var cssText = new StringBuilder();
cssText .Append(“position:absolute;left:”).Append(iLeft).Append( “;top:”).Append(iTop).Append( “;”);
document.all[“”].style.cssText = cssText.ToString();

//Solveing example 3
var iTop = 50;
var iLeft = 50;
var iWidth = 800;
var iHeight = 600;

var windowFeature = new StringBuilder();
windowFeature.Append(“toolbars=0,”);
windowFeature.Append(“width=”).Append(iWidth).Append( “,”);
windowFeature.Append(“height=”).Append(iHeight).Append( “,”);
windowFeature Append(“top=”).Append(iTop).Append(“,”);
windowFeature.Append(“left=”).Append(iLeft);

window.open( “” , “” , windowFeatures.ToString());
}

OnCrmPageLaod();

The second code snippet contains an extension function to the built-in JavaScript string object which helps you solve the problem mentioned in example 1 above and also makes it possible to write code similar to the String.Format c# method.

String.prototype.Format = function( args )
{
var result = this.replace( /\{(\d{1})\}/ig ,
function match()
{
return args[arguments[1]];
}
);

return result;
}

function OnCrmPageLoad()
{
//Solve Exampe 1
var someValue = new StringBuilder();
someValue.Append(“The account number: {0} is not valid\n”.Format([crmForm.accountnumber.DataValue]));
someValue.Append(“More helpful info on how to fill a valid account number here.”);

alert(someValue.ToString())

//Solve Example 2
var iTop = 50;
var iLeft = 50;
var iWidth = 800;
var iHeight = 600;

//Create string template with place holders
var windowFeature = “toolbars=0;top={0},left={1},width={2},height={3}”;
//Format the String
windowFeature = windowFeature.Format([iTop,iLeft,iWidht,iHeight]);
window.open( “” , “” , windowFeatures);
}

OnCrmPageLoad();

The last example extends the StringBuilder class and creates a StyleBulder that is used to concatenate CSS rules with ease

StyleBuilder = function()
{
var cssText = new StringBuilder();
this.Add = function( key , value ){cssText.Append( key ).Append( ":" ).Append( value ).Append( ";" );}
this.ToString = function(){return cssText.ToString();}
}

//Usage
function OnCrmPageLoad()
{
//Solve Example 2
var iLeft = 100;
var iTop = 100;
var cssText = new StyleBuilder();
cssText.Add(“position”,”absolute”);
cssText.Add(“left”, iLeft);
cssText.Add(“top”,iTop);

document.all[“”].style.cssText = cssText.ToString();
}

OnCrmPageLoad();

I hope you find this interesting and helpful.


Источник: http://mscrm4ever.blogspot.com/2009/...h-strings.html
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
Microsoft Dynamics CRM Team Blog: CRM Online: Reporting Options Blog bot Dynamics CRM: Blogs 0 18.06.2009 06:14
Microsoft Dynamics CRM Team Blog: Building a Self-Contained Virtual CRM Development Server Blog bot Dynamics CRM: Blogs 0 05.05.2009 10:05
Microsoft Dynamics CRM Team Blog: Building Rich-Client Dashboards for Microsoft Dynamics CRM with Windows Presentation Foundation Blog bot Dynamics CRM: Blogs 1 31.03.2009 13:24
Microsoft Dynamics CRM Team Blog: List Web Part for Microsoft Dynamics CRM 4.0 Deployment Scenarios Blog bot Dynamics CRM: Blogs 0 30.01.2009 22:05
Microsoft Dynamics CRM Team Blog: Microsoft Dynamics CRM 4.0 Bookshelf Blog bot Dynamics CRM: Blogs 1 22.01.2009 04:46

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 13:44.