Execute javascript functions

With JavaScriptModel you can easily execute javascript functions from code behind with the following extension methods.

AddJavaScriptFunction

AddJavaScriptFunction adds a javascript function call to the end of the queue.
You can add variables of any primitive or complex type as parameter. The order should be the order they are defined in javascript.
The PageLoadEvent defines when the function should be executed in javascript.
PageLoadEvent.Init executes the function as soon as possible (DOM could be not loaded!)
PageLoadEvent.Ready executes the function 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 AddJavaScriptFunction(
				this IJavaScriptModelAware javaScriptModelAware,
				string function,
				PageLoadEvent pageLoadEvent = PageLoadEvent.Ready,
				IDataStoreProvider dataStoreProvider = null,
				params object[] functionParameters)

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

public ActionResult Index()
{
    this.AddJavaScriptFunction("JavaScriptVariablesController.Init", PageLoadEvent.Init);

    this.AddJavaScriptFunction("JavaScriptVariablesController.Ready");

    this.AddJavaScriptFunction("SomeGlobalFunction", "this is a parameter value", 123);
}

InsertJavaScriptFunction

InsertJavaScriptFunction adds a javascript function call to the begin of the queue.
You can add variables of any primitive or complex type as parameter. The order should be the order they are defined in javascript.
The PageLoadEvent defines when the function should be executed in javascript.
PageLoadEvent.Init executes the function as soon as possible (DOM could be not loaded!)
PageLoadEvent.Ready executes the function 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 InsertJavaScriptFunction(
				this IJavaScriptModelAware javaScriptModelAware,
				string function,
				PageLoadEvent pageLoadEvent = PageLoadEvent.Ready,
				IDataStoreProvider dataStoreProvider = null,
				params object[] functionParameters)

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

public ActionResult Index()
{
    this.AddJavaScriptFunction("JavaScriptVariablesController.Init", PageLoadEvent.Init);

    this.AddJavaScriptFunction("JavaScriptVariablesController.Ready");

    this.AddJavaScriptFunction("SomeGlobalFunction", "this is a parameter value", 123);
}

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

public ActionResult Index()
{
    this.AddJavaScriptFunction("SomeFunction");

    this.InsertJavaScriptFunction("AnotherFunction");

    // --> AnotherFunction will be called first
}

Last edited Jan 7, 2013 at 12:35 PM by acuntex, version 2

Comments

No comments yet.