Oct 09

Getting started with bindings – part III

Posted in Uncategorized      Comments Off on Getting started with bindings – part III

[Update: removed null parameters…after all this is JavaScript, not C#. Thanks, once again, go to Bertrand]

In the previous post, we’ve seen that we can use JavaScript to create a binding between two objects. At the time, I’ve mentioned two important things: bindings are represented by Sys.Binding objects and bindings are also MS AJAX components (since the “Sys.Binding” type expands the Sys.Component type). One of the things that I’ve mentioned was that you needed to call the initialize method to ensure that everything works ok.

As Bertrand said in the comments (and believe me, you should always listen carefully to what he says :),,), you can reduce the JavaScript code by using the $create helper. That means that we can create a new binding by using a simple method call:

<script type="text/javascript">
    function pageLoad() {
                    source: $get("source"),
                    path: "value",
                    target: $get("target"),
                    targetProperty: "value",
                    id: "testBinding"

Notice that I’ve also set the id property. That means that we can use the $find helper if we need to get a reference to this binding object later (notice that there’s another option here: you could use a global variable and save the return result of the $create method – don’t forget that the $create always returns a reference to the component it creates).

I do recommend this approach for creating bindings (or any sort of MS AJAX components for that matter). Besides reducing the code, it will always ensure that you initialize the component (something that most people end up forgetting after initializing the properties of the object).

And that’s it. We’ll go back to interesting stuff in the next post. Stay tuned for more on MS AJAX.