Overview

ActiveWAFL provides a few global functions that can come in handy.

Start

Use Start() to make sure that your code does not run until the DOM is fully loaded.
You can use Start anywhere in your application, as many times as you'd like.
Multiple calls to Start results in a queue that will run your code in the same order that Start was executed.
Hello world using the Start function
  1. Start
  2. (
  3.    function()
  4.    {
  5.       alert("Hello world!");
  6.    }
  7. );

Selectors

Use selectors to get elements from the DOM.
$
Get an element by id.
Get the element with an id of Username
  1. var elem = $("Username");
$$
Get all elements of a certain tag type
Get all sections on the page
  1. var sections = $$("section");
$class
Get the first element with the matching class
Get the first product on the page
  1. var product = $class("product"); //or...
  2. var product = $c("product"); //You can use c for short
$$class
Get all of the elements with the matching class
Get all products on the page
  1. var products = $$class("product"); //or...
  2. var products = $$c("product"); //You can use c for short
$query
Get the first element with the matching selectors
Get the first product section on the page
  1. var product = $query("section.product"); //or...
  2. var product = $q("section.product"); //You can use q for short
$$q
Get all of the elements with the matching selectors
Get all product sections on the page
  1. var products = $$query("section.product"); //or...
  2. var products = $$q("section.product"); //You can use q for short

XHR / Ajax

Asynchronous Requests

Use asynchronous requests to make an HTTP request without waiting for the response.
When it is ready, the response will be sent to a callback that you specify.

DblEj.Communication.Ajax.Utils.SendRequestAsync
Shorthand alias: _ (a single underscore)
SendRequestAsync signature
  1. _(String call, [Object requestObject, String sendToken, Function callbackMethod, String method, Boolean useDefaultJsonParser]);
Get a tree of hypotheitcal categories
  1. _("Category.GetTree", { "Depth":3 }, null,
  2.    function(responseObject, stateToken)
  3.    {
  4.       console.log(responseObject);
  5.    });

Synchronous Requests

DblEj.Communication.Ajax.Utils.SendRequest

Use synchronous requests if you want to stop and wait for the response before continuing execution of your code.

Shorthand alias: __ (two underscores)
SendRequest signature
  1. __(String call, [Object requestObject, String method, Boolean useDefaultJsonParser]);
Get the current server GMT time by calling the hypothetical Server.GetTime API which accepts an argument called Timezone
  1. var currentTime = __("Server.GetTime", { "Timezone":"GMT" });

File Uploads

DblEj.Communication.Ajax.Utils.SendFile

Use SendFile to upload a file asynchronously using HTTP.

Shorthand alias: ___ (three underscores)
SendFile signature
  1. ___(String call, file, [Object requestObject, Function progressCallback, Function completedCallback]);
Upload a file to the hypothetical Category.UploadMedia API which accepts an argument called MakeFilePublic
  1. var currentTime = ___("Category.UploadMedia", $("FormFileInputElement"){ "MakeFilePublic":true },
  2.                   function(event)
  3.                   {
  4.                      console.log("bytes loaded: "+event.loaded);
  5.                   },
  6.                   function(resultCode, response, sentGetVariables)
  7.                   {
  8.                      if (resultCode == 200) //HTTP OK
  9.                      {
  10.                         console.log(response);
  11.                      } else {
  12.                         //handle non-ok result
  13.                      }
  14.                   };

Form Submission

DblEj.Communication.Ajax.Utils.SendForm

Use SendForm to upload a form asynchronously using HTTP.

Shorthand alias: ____ (four underscores)
SendForm signature
  1. ____(String call, form, [Function completedCallback, Object requestObject, String sendToken, Boolean useDefaultJsonParser]);
Upload a form to the hypothetical Category.UploadForm API.
  1. var currentTime = ____("Category.UploadForm", $("FormId")
  2.                   function(resultCode, response, sentGetVariables)
  3.                   {
  4.                      if (resultCode == 200) //HTTP OK
  5.                      {
  6.                         console.log(response);
  7.                      } else {
  8.                         //handle non-ok result
  9.                      }
  10.                   };

Testing Variables

IsArray

Returns true if the tested variable is an array
  1. var s = "test";
  2. var a = [];
  3.  
  4. console.log(IsArray(s)); //returns false
  5. console.log(IsArray(a)); //returns true

IsDefined

Check if caller passed anything for the z argument
  1. function (x, y, z)
  2. {
  3.    alert (IsDefined(z)?"Yes, z is defined":"No, z is not defined");
  4. }

IsNullOrUndefined

Returns true if the caller passed null for the z argument or didn't pass anything for the z argument
  1. function (x, y, z)
  2. {
  3.    alert (IsNullOrUndefined(z)?"Yes, z is null or undefined":"z is defined and z is not null");
  4. }

IsNumeric

Returns true if the tested variable can be evaluated as a number
  1. var s = "test";
  2. var s2 = "123.5";
  3.  
  4. console.log(IsNumeric(s)); //returns false
  5. console.log(IsNumeric(s2)); //returns true

Iteratables

You can make arrays and objects iteratable with a standard, and easy to use, interface.

Iteratize

Pass any array or object to this function to make it iteratable.
For arrays, the iterator will iterate over it's elements.
For objects, the iterator will iterate over it's properties and methods.

OnEach

Once iteratized, the object will have a new method call OnEach.
You can use this method to iteratize over the public members.

Iteratize an array and iterate over its elements using OnEach
  1. var a = ["test","test2","test3"];
  2. Iteratize(a);
  3.  
  4. a.OnEach
  5. (
  6.    function(s)
  7.    {
  8.       console.log(s);
  9.    }
  10. );
  11.  
  12. //returns:
  13. //test
  14. //test2
  15. //test3

Window

Use these helper functions to handle common tasks quickly.

GetOffsetPoint

Get the window scroll position for the current viewport.

GetOffsetPoint signature
  1. var offsetPoint = window.GetOffsetPoint();
  2.    console.log("ScrollX="+offsetPoint.Get_X()+", ScrollY="+offsetPoint.Get_Y()

GetSize

Get the size of the window.

GetSize signature
  1. var size = window.GetSize();
  2.    console.log("Width="+offsetPoint.Get_X()+", Height="+offsetPoint.Get_Y()

window.LoadNewPage

Navigate to the specifed URL.

LoadPageWithDelay signature
  1. window.LoadNewPage(String pageUrl)

window.LoadNewPageInNewWindow

Open a new window and navigate to the specifed URL.

LoadNewPageInNewWindow signature
  1. window.LoadNewPageInNewWindow(String pageUrl)

window.LoadPageWithDelay

After the specified delay period, navigate to the specified URL.
If specified, show the showDivElem element during the delay period (such as a loading icon).

LoadPageWithDelay signature
  1. window.LoadPageWithDelay(String pageUrl,Number delayMs,Element showDivElem)

window.OpenLinkWithUserConfirmation

Ask the user for confirmation and, if confirmed, navigate to the specified URL.

OpenLinkWithUserConfirmation signature
  1. window.OpenLinkWithUserConfirmation(String caption,String link)

window.ShowPopup

Show a popup window

ShowPopup signature
  1. window.LoadNewPageInNewWindow(String url, String referenceId, Number width, Number height)

Misc

Function.Bind

Use the Bind method on a function to specify the scope in which the function will run.
This determines what the value of "this" will be when the method runs.

Function.Bind signature
  1. Function.Bind(Object thisObject);
Function.Bind example
  1. var someVariable = "This is some variable";
  2.    $("SomeElement").AddClickHandler(
  3.    function(elem)
  4.    {
  5.       console.log(this); //will print "This is some variable"
  6.    }.Bind(someVariable));