How to expose global variables in JavaScript

Go To


What is the best approach for exposing a global variable from an MVC Layout Master to a child view.

Here's the flow:


var myPlugin = $("#myPlugiv").MyPlugin();

Child View:

var returnValue = myPlugin.GetValue();

This obviously works since the myPluin variable is visible to the child view, but is there another recommended pattern for sharing global values between Master Layout and the child view?

2012-04-04 01:15
by TGH


You could always organize them into a namespace to limit the number of globally scoped variables. Something like:

window.ViewModel = {
    myPlugin: $('#myPlugin').MyPlugin()

var returnValue = ViewModel.myPlugin.GetValue();
2012-04-04 02:05
by Scott Hamper
I like that ide - TGH 2012-04-04 02:42