TGH
TGH

Reputation: 39268

How to expose global variables in JavaScript

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

Here's the flow:

Layout:

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?

Upvotes: 0

Views: 1616

Answers (1)

Scott Hamper
Scott Hamper

Reputation: 381

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();

Upvotes: 2

Related Questions