This project is read-only.

Including Other Dynamic Javascript Modules

Requires knockout-x.x.x.js,  knockout.mapping-x.x.x.js, MvcControlToolkit.Bindings-x.x.x.js and MvcControlToolkit.Utils-x.x.x.js

A javascript dynamic file may include other dynamic files by using the helper:

MvcHtmlString IncludeStaticJsModules(
       this HtmlHelper htmlHelper, 
       params string[]  partials)

 

The array of strings contains the names of all partial views with javascript code to include:

 

 @Html.IncludeStaticJsModules("Home/MenuJs", 
            "Home/RegisterJs", "Home/LoginJs", 
            "Home/LogoutJs", "Home/RegistredJs")

 

All included files are copied in the final file deployed to the client during the rendering.

Each included module may return a value to the including module by calling the javascript function:

mvcct.core.moduleResult(<value to return>)
In turn the including module may get all values returned by its included module by calling the function:

@Html.IncludeStaticJsModules("Home/MenuJs", 
            "Home/RegisterJs", "Home/LoginJs", 
            "Home/LogoutJs", "Home/RegistredJs")
mvcct.core.withIncludedModules(
    function (menuF, registerF, loginF, logoutF, registredF) {
            ...
            ...
    }
});

 

The function contained in the mvcct.core.withIncludedModules method is automatically passed all values returned by the modules previously included with @Html.IncludeStaticJsModules.
mvcct.core.withIncludedModules returns the same value returned by function (menuF, registerF, loginF, logoutF, registredF)....

Below a complete example of an AMD module (it starts with a define) that includes several other modules taken from a Data Moving SPA View Engine Example:

<script> 
    define(function () {
        @Html.IncludeStaticJsModules("Home/MenuJs", 
            "Home/RegisterJs", "Home/LoginJs", "Home/LogoutJs", "Home/RegistredJs")
        return mvcct.core.withIncludedModules(
            function (menuF, registerF, loginF, logoutF, registredF) {
              return function (vm, viewName) {
                    var context = mvcct.ko.dynamicTemplates.getContext(vm);
                    vprefix = context ? context.vprefix : '';
                    if (viewName == "Menu") return menuF(vm);
                    else if (viewName == "Register") return vprefix == "Logged" ? 
                            registredF(vm) : registerF(vm);
                    else if (viewName == "Login") return vprefix == "Logged" ? 
                            logoutF(vm) : loginF(vm);
                    return null;
            }
        });
    });  
</script>

Last edited Jun 22, 2014 at 11:37 AM by frankabbruzzese, version 7

Comments

No comments yet.