Transfer variables to javascript

With JavaScriptModel you can easily transfer data from code behind to javascript with the following extension methods.

AddJavaScriptVariable

AddJavaScriptVariable adds a single variable to the given javascript variable name. This variable can be of any primitive or complex type.
The PageLoadEvent defines when the variable should be added to javascript.
PageLoadEvent.Init adds the variable as soon as possible (DOM could be not loaded!)
PageLoadEvent.Ready adds the variable when the DOM has been loaded completely. This is the default PageLoadEvent.
It is possible to create an own data store provider implementing the IDataStoreProvider-interface where the JavaScriptModel will be stored for the request.

public static void AddJavaScriptVariable(this IJavaScriptModelAware javaScriptModelAware,
					string variableName,
					object value,
					PageLoadEvent pageLoadEvent = PageLoadEvent.Ready,
					IDataStoreProvider dataStoreProvider = null);

Here are some examples how to use AddJavaScriptVariable in an action:

public ActionResult Index()
{
    this.AddJavaScriptVariable("SomeGlobalVariable", "This is some global variable");

    this.AddJavaScriptVariable("ExistingLiteral.SomeVariable", 42);

    this.AddJavaScriptVariable("ExistingLiteral.AnotherLiteral.SomeVariable", true);

    this.AddJavaScriptVariable("NotExistingLiteral.SomeModel",
            new SomeModel(108, "This is an object transfered to javascript."));

    this.AddJavaScriptVariable("ExistingLiteral.SomeInitValue",
            "This value has been set before ready() was fired.", PageLoadEvent.Init);
}

AddJavaScriptVariables

AddJavaScriptVariables adds multiple variables in a single method call to the given variable names. These variables can be of any primitive or complex type.
The PageLoadEvent defines when the variable should be added to javascript.
PageLoadEvent.Init adds the variable as soon as possible (DOM could be not loaded!)
PageLoadEvent.Ready adds the variable when the DOM has been loaded completely. This is the default PageLoadEvent.
It is possible to create an own data store provider implementing the IDataStoreProvider-interface where the JavaScriptModel will be stored for the request.

public static void AddJavaScriptVariables(this IJavaScriptModelAware javaScriptModelAware,
					PageLoadEvent pageLoadEvent = PageLoadEvent.Ready,
					IDataStoreProvider dataStoreProvider = null,
					params object[] variables);

Here are some examples how to use AddJavaScriptVariables in an action:

public ActionResult Index()
{
    this.AddJavaScriptVariables("SomeGlobalVariable", "This is some global variable",
        "ExistingLiteral.SomeVariable", 42,
        "ExistingLiteral.AnotherLiteral.SomeVariable", true,
        "NotExistingLiteral.SomeModel", new SomeModel(108, "This is an object transfered to javascript."));

    this.AddJavaScriptVariables(PageLoadEvent.Init,
        "ExistingLiteral.SomeInitValue", "This value has been set before ready() was fired."
        "ExistingLiteral.AnotherInitValue", 123);
}

Last edited Jan 7, 2013 at 1:20 PM by acuntex, version 3

Comments

No comments yet.