Dynamic Javascript Utilities

There are a few helper extension methods that serialize .Net data structures into Javascript data structures.

 

public static MvcHtmlString JavaScriptDefine(
       this HtmlHelper htmlHelper, 
       string variableName, 
       object data)

 

It defines the javascript variable variableName and assign it the javascript data structure obtained by the serializing the .Net data structure data.  

 

public static MvcHtmlString JavaScriptAssign(
       this HtmlHelper htmlHelper, 
       string variableName, 
       string path, 
       object data)

 

 

 

If path is null the javascript data structure obtained by the serializing the .Net data structure data is assigned to the already defined javascript variable variableName.
If path is not null it must be a string of the type "property.subproperty....". In this case the javascript data structure obtained by the serializing the .Net data structure data is assigned to the property or sub-property specified by path in the javascript object that MUST already be contained in the javascript variable variableName. If some sub-property of path is undefined or null an object is automatically created and inserted in that property.
 
public static MvcHtmlString JsonString(this HtmlHelper htmlHelper, object data)


it returns the javascript data structure obtained by serializing the .Net data structure data.

Below an example of usage of JavaScriptDefine:
 
<script>   
    (function () {
        @Html.JavaScriptDefine("mainMenu",  mainMenu) 
        mvcct.core.moduleResult(function (vm) {
            if (!vm._initialized) {
                vm._initialized = true;
                vm.Content = ko.mapping.fromJS(mainMenu);
                vm._title = "@SiteResources.HomeTitle";
            }
            return null;
        });
    })();  
</script>

 

Last edited Jun 22, 2014 at 10:34 AM by frankabbruzzese, version 2

Comments

No comments yet.