Sending complex typed data from web service to SilverLight client - Part III
Sending complex typed data from web service to SilverLight client - Part III
On my previous post I discussed the JavaScript mediator part,
Here I'll sum this part which is Serialize the object for the SilverLight
component and operate method inside it to bind the data.
Step 5 - Serialize the data for the SilverLight component
And here is the code converting the objects
function onGettingAllEmployeesSuccess(result)
{
var slEmployees = new Array(result.length);
for (var index = 0; index <result.length; index++)
{
var newEmployee =
{
"FirstName" : result[index].FirstName,
"LastName" : result[index].LastName,
"EmployeeNumber" : result[index].EmployeeNumber
};
slEmployees[index] = newEmployee;
}
//Serialize the employees
var slSerEmployees = Object.toJSON(slEmployees);
//Bind the data on the silverlight component
$get("Xaml1").Content.silverlightManagedCode.BindListBox(slSerEmployees);
}
The method Object.toJSON is taken from the file Prototype.js written by
Sam Stephenson and distributed freely on http://www.prototypejs.org/
And Finally - Let's insert our SilverlLight control into our test page:
...
<asp:Silverlight ID="Xaml1" runat="server"
Source="~/ClientBin/<YourSilverLightProjectName>.xap"
Version="2.0" Width="100%" Height="100%" />
...
Summary: In order to have complex typed data sent from
your server to your Cleint's SilverLight control via JavaScript you
need to do the following:
1. On your SilverLight project code - write a class with data corresponds
to the data you get from your server.
2. Write JavasCript mediator between your page and your SilverLight control
3. Operate method from your page on your SilverLight control to bind
the serialized data received.
I've attached the SilverLight project's code so you can get started
with your server & mediator easily.