ASYNergy Library

ASYNergy is a complement to revIgniter. It is a JavaScript application inspired by and adopted from Livewire. It is a framework for making network requests and changing things on the page. Its name is a compound of the words "asynchronous" (Asynchronous JavaScript and XML) and "synergy", which means the synergy between the two frameworks ASYNergy and revIgniter.

Note: If the server engine used is not LC Create, this library requires the LiveCode Builder (library) extension com.livecode.library.json which needs to be stored in application/extensions. So, the path to the extension is application/extensions/com.livecode.library.json/module.lcm. Please read about how to include the LiveCode JSON extension in chapter "Extensions". The library loads the JSON extension automatically.

Note: ASYNergy's current state is pre-release/beta-release, it is not ready for production use. Serious bugs and unexpected behavior cannot be ruled out.

Table of Contents

 

Overview

Before outlining each library handlers, let's describe revIgniter's relationship with ASYNergy and how to embed ASYNergy code in your view file.

Note: Please read about the ASYNergy JavaScript framework and get detailed information on the ASYNergy wiki page.

Top of Page

Tutorials

To learn how ASYNergy works, there are 3 tutorials available:

Initializing the Library

Like most other libraries in revIgniter, the ASYNergy library is initialized in your controller using the rigLoaderLoadLibrary handler:

rigLoaderLoadLibrary "ASYNergy"

Please note that there is no need to load the asset helper in your controller. The ASYNergy library loads it automatically.

Handler Reference

The following ASYNergy library handlers are intended for use in your controller handlers.

rigAsynElemData(pAttrVal)

This function returns the value, innerText, innerHtml etc. of an HTML element specified by the elements's ASYNergy directive (attribute) value. If dealing with select elements this function returns an array.

Parameters

In the following example, this function returns the value of an input field that triggers an AJAX request and calls the "name" handler when the user enters something in the field:

command name
  local tMutableVal

  # SET THE LETTERS OF THE VALUE OF THE asyn:model ELEMENT
  # WHOSE ATTRIBUTE VALUE IS "name" TO UPPERCASE
  put toUpper(rigAsynElemData("name")) into tMutableVal

  # SEND THE RESPONSE INCLUDING THE MODIFIED INPUT DATA
  rigAsynRespond tMutableVal
end name
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>

  <body>
    <div style="float: left;margin-right: 20px;">
      <input asyn:model="name" type="text">
    </div>
        
    <div>
      <p>Hello <span asyn:mutable="name"></span></p>
    </div>

    [[gData["asynergyScript"] ]]
  </body>
</html>

Top of Page

rigAsynRespond pRespData, pSyncModelID, pMutableID, pMutableAttrVal

This handler prepares the ASYNergy response and sends it to the client in the form of JSON data.

Parameters

Examples:

In its simplest form this function takes the value of "model" elements and adds it to the response if you enclose the asyn:model value in double curly braces:

command name
  rigAsynRespond "{{name}}"
end name
<input asyn:model="name" type="text">
<p>Hello <span asyn:mutable="name"></span></p>

If you like to keep the "model" in sync with the "mutable" element, add an id attribute to the "model" and pass the value of the id attribute as second parameter of the rigAsynRespond handler:

command name
  put toUpper(rigAsynElemData("name")) into tMutableVal
  rigAsynRespond tMutableVal, "theName"
end name
<input asyn:model="name" type="text" id="theName">
<p>Hello <span asyn:mutable="name"></span></p>

This example shows how to address particular "mutable" elements that have the same asyn:mutable value, in this case "foo":

command foo
  # THIS WILL UPDATE THE MUTABLE ELEMENTS WITH ID "bar" AND "baz"
  # BECAUSE THE PARAMETERS OF THE BUTTON DIRECTIVE asyn:click="foo('bar','baz')"
  # ARE "bar" AND "baz"
  put rigAsynParams() into tParamsA
  rigAsynRespond "-" & the secs & "-", , tParamsA
end foo
<p asyn:mutable="foo" id="bar">[[gData["secs"] ]]</p>
<p asyn:mutable="foo" id="baz">[[gData["secs"] ]]</p>
<p asyn:mutable="foo" id="test">[[gData["secs"] ]]</p>
<button asyn:click="foo('bar','baz')">Update two</button>

This example shows how to address particular "mutable" elements identified by their mutable attribute values, in this case "foo" and "test":

command updateAll
  # THIS WILL UPDATE THE MUTABLE ELEMENTS WITH asyn:mutable VALUES
  # "foo" AND "test" BECAUSE THE PARAMETERS OF THE BUTTON DIRECTIVE
  # asyn:click="updateAll('foo','test')" ARE "foo" AND "test"
  local tParamsA
  
  put rigAsynParams() into tParamsA
  
  rigAsynRespond "-" & the secs & "-", , , tParamsA
end updateAll
<p asyn:mutable="foo" id="bar">[[gData["secs"] ]]</p>
<p asyn:mutable="foo" id="baz">[[gData["secs"] ]]</p>
<p asyn:mutable="foo" id="bla">[[gData["secs"] ]]</p>
<p asyn:mutable="test">[[gData["secs"] ]]</p>
<button asyn:click="updateAll('foo','test')">Update all</button>

Top of Page

rigAsynParams(pParamKeys)

This function returns parameters of asyn:model, asyn:click, asyn:keydown and asyn:submit directives as a numbered array.

Parameters

In this example, clicking the "Reset name" button replaces the value of the input field with the second parameter of the asyn:click directive, set in uppercase.

# REQUEST FROM INPUT FIELD
command name
  rigAsynRespond "{{name}}"
end name


# REQUEST FROM BUTTON "Reset Name"
command resetName
  local tParamsA
  
  # GET PARAMETERS OF THE asyn:click DIRECTIVE
  # OF THE "Reset Name" BUTTON AND SPECIFY
  # TO WHICH PARAMETER(S) AN LC FUNCTION SHOULD
  # BE APPLIED ACCORDING TO THE VALUE OF THE
  # asyn:mutable DIRECTIVE USING DOT NOTATION,
  # IN THIS CASE THE FUNCTION IS toUpper()
  put rigAsynParams(2) into tParamsA

  rigAsynRespond tParamsA[2], "name"
end resetName
<div style="float: left;margin-right: 20px;">
  <input asyn:model="name" type="text">
</div>
  
<div>
  <p>Hello <span asyn:mutable="name.toUpper"></span></p>
  <button asyn:click="resetName('Bingo','Chico')">Reset Name</button>
</div>

Top of Page

rigAsynPayload(param1, param2)

This function allows access to all values of the ASYNergy payload sent to the server. Returned values are strings or arrays.

Parameters

Example:

# GET THE INNER HTML OF A "mutable" ELEMENT
# THAT IT HAD BEFORE THE AJAX REQUEST
put rigAsynPayload("mutablesData", "0") into tMutableElementA
put tMutableElementA["mutableInnerHTML"] into tPreviousHTML

Note: The names of the payload array keys can be found in the ASYNergy request data in your browser using the developer tools.

Top of Page

rigAsynValidateInput(pModelAttrVal, pTransmittedElA, pRuleGroup)

Validate user input data using the form validation library.

Parameters

This example shows how an input field is validated when it has lost focus. This is done by using the "blur" modifier. The asyn:transmit directive of the input field is needed when the user clicks the submit button.

If the validation fails an error message will be added to the response which is sent by the rigAsynRespond handler. This message replaces the current HTML enclosed by the tags of a "mutable" element whose asyn:mutable attribute has the same value as the asyn:model attribute and the asyn:transmit attribute.

command email
  local tError

  put rigAsynValidateInput("email") into tError
  rigAsynRespond tError
end email
<form action="https://mysite.com/asynRegister" method="post" asyn:submit.prevent="signup">

<label for="email">Email Address</label>
<input type="text" name="email" asyn:model.blur="email" asyn:transmit="email" value="[[gData["email"] ]]">

<p class="errorMessage" asyn:mutable="email"></p>

<input name="register" value="Register" type="submit"  />
</form>

Note: This function needs the Formvalidation library.

To get a better understanding of form validation with ASYNergy, please read the ASYNergy Form Validation tutorial.

Top of Page

rigAsynNavigate pSegments

Use this handler to navigate to another page.

Parameters

Example:

command signup
  local tErrorA, tSegments
  
  put rigAsynValidateInput("email,password,passwordConfirm") into tErrorA
  
  if tErrorA is empty then
    # DISPLAY SUCCESS PAGE
    put "asynRegister/success" into tSegments
    rigAsynNavigate tSegments
  else
    rigAsynRespond tErrorA
  end if
end signup


command success
  get rigLoadView("registerSuccessView")
end success

Top of Page

rigAsynAddResponseData(pRespData, pSyncModelID, pMutableID, pMutableAttrVal)

This function accumulates response data. Use this function if there are multiple "mutable" ASYNergy HTML elements and you want the HTML data enclosed by the tags of the individual elements to be updated with different data.

Parameters

Example:

put getMsgData() into tMsgList

# IF YOU DON'T INTEND TO CLEAR THE USER INPUT FIELD
# CALL rigAsynRespond tMsgList AND YOU ARE DONE
# OTHERWISE...
put rigAsynAddResponseData(tMsgList, , , "addMsg") into tResponseA
put rigAsynAddResponseData("", , , "userinput") into tResponseA

rigAsynRespond tResponseA

You can find a use case for this function in the Simple Chat Application tutorial.

rigIsAsynRequest()

This function returns true or false depending on whether the request was an ASYNergy request.

Top of Page