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

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 30.12.2014, 23:51   #1  
Blog bot is offline
Blog bot
Участник
 
25,644 / 848 (80) +++++++
Регистрация: 28.10.2006
Many cool things often go undocumented. I’ve just stumbled upon one of those, and it comes in handy to close this year of blogging.

Imagine this situation: you have a server-side .NET object, that you want to pass on to the client. With the .NET System.Windows.Forms-based objects, you have to make the object serializable, deploy the object to the client-side Add-in folder, and then set the RunOnClient property on the C/AL variable to Yes.

However, if the client is not .NET-based, if it is a cross-client JavaScript-based one. You may think that it’s not possible to pass the custom object on to JavaScript code. And you may be wrong.



One of many beauties of JavaScript is that it can construct objects on the fly, using JSON notation. For example, if you want to have an object to represent a person, you can just do this:

var person = { FirstName: 'John', LastName: 'Doe'};



.NET is not as flexible, it requires you to declare a class first. C/AL is even less flexible, because it doesn’t even have classes.

But imagine that you had the following C# class that you used on both client and server side in C/AL:

[Serializable]public class Person{ public string FirstName {get; set; } public string LastName { get; set; }}



To pass it on to JavaScript, in your control add-in interface, simply declare a method:

void SendPerson(Person person);

Then, in your JavaScript script file, define the function:

function SendPerson(person) { alert("Hello, " + person.FirstName + " " + person.LastName);};



And then, you don’t need to do anything at all. NAV runtime will take care of everything. As a matter of fact, you don’t need to have the assembly deployed to the client add-ins folder at all.

Why does it work?

It’s simple. As long as your class is serializable (for example, by decorating it with the [Serializable] attribute), the NST first serializes it to XML. Then, the client-side runtime (either the Web client ASP.NET application, or the Windows client) translates the XML to JSON, and then passes it on to your function as native JavaScript object.

And again – no deployment to client-side is required.

Pretty cool, if you ask me. I haven’t tested it with everything, but I can make an educated guess here: as long as it is serializable to XML, and as long as all data types you use can be mapped to JavaScript data types, you can pass just about anything.

This gives you a lot of flexibility when interacting between the client and the server, and it also allows you to port your .NET-based control add-ins to JavaScript much easier.

Read this post at its original location at http://vjeko.com/blog/passing-strong...pt-control-add, or visit the original blog at http://vjeko.com. 5e33c5f6cb90c441bd1f23d5b9eeca34The post Passing strongly typed data to a JavaScript control add-in appeared first on Vjeko.com.

</img> </img>

Читать дальше
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 


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

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

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