views:

30

answers:

2

I am developing an asp.net mvc web application that makes much use of jquery and the progressive enhancement principle.

I have a requirement for a complex control that needs access to .net code in order to function (using reflection across numerous classes). The choices were to duplicate this code in javascript (auto-generate if possible), or use silverlight. I am going with the silverlight option (at least for V1).

The pre-silverlight version of my control starts life as some simple html elements that are modified by jquery into the richer version. However the examples I've seen of silverlight controls all call back to the server once the page has loaded to get their data. Or, if this data is simple, it uses the initParams parameter.

My initialisation data is much more complicated that I think initParams can work with and I don't want to call back to the server as I already have this data.

So what are my best options?

+1  A: 

You can put whatever you want into initParams. It is just a string, but if you have a complex object you want to pass to the silverlight control then you can serialize the object into a string, whack it into initParams, and then deserialize it within the silverlight control.

Alternatively, you do know that you can communicate from javascript into managed code in the silverlight control (the functions have to be marked with the ScriptableMember attribute)?

Not to mention that you shouldn't be concerned with calls from the silverlight control back to a WCF or asmx webservice - in my experience the calls are very fast unless you are transferring large quantities of data.

slugster
Thanks for your response. Serialising an object into a string to put into the silverlight object as an attribute would work but it sounds a bit icky to me. Is this common practise? I wasn't really concerned about the time delay for the callback, just that I am pulling data out of the database, putting it on a web page and then enriching it on the client. I don't want to break that model just for one control? The javascript method was where I was looking I just wanted to know what was commonly done or whether I was missing anything.
Chris Simpson
@Chris: Its difficult to define what is "common" since SL is relatively new and still rapidly evolving. However putting large amounts of data in initParams does have a "bad smell" about it, I wouldn't do it myself. What might be more typically (if such a thing exists already) in your scenario is an AJAX HTML page that enriches itself with a Silverlight plugin. In both cases the request for the data happens separately from the loading of presentation code and would mostly likely share a common service providing the data.
AnthonyWJones
Serialising the object into the initParams isn't common practice, it's just an option. initParams is designed for basic stuff that your silverlight control absolutely needs to get started. Personally i would rather rely on the aforementioned webservice call, whether directly from the silverlight control or from ajax and a javascript bridge.
slugster
+1  A: 

In the scenario you describe you can use the HTMLBridge in Silverlight to call a javascript function in your page that can provide the complex data. Silverlight has libraries for handling both XML and JSON, however, Silverlight data binding becomes much more usable when you load that data into a strongly typed model.

AnthonyWJones
This sounds very interesting thanks. I already have my objects as js classes so I can get silverlight to call a js function, get these values and set itself up. I'll look into it.
Chris Simpson