Javascript Functions
Javascript Functions
Appcelerator has several Javascript functions that you may find useful while developing applications with Appcelerator.
Message Publish and Subscribe Functions
| $MQ([message],[message payload]) | $MQ allows you to publish a message via Javascript. The first parameter is the message (e.g., "l:show.calendar"). The second parameter is the message payload. The message payload should be JSON-based. It's an optional parameter. |
| $MQL([message],[callback function]) | $MQL allows you to subscribe to a message via Javascript. The first parameter is the message (e.g., "l:show.calendar"). The second parameter is the callback function. |
Here’s an example of the $MQ function
$MQ("l:show.calendar",{"username":"foo"});
Here’s an example of the $MQL function
$MQL("l:show.calendar",function(type,msg)
{
// your code
});
You callback function is passed the message type (type) and the message data payload (msg).
Compiler Functions
| Appcelerator.Compiler.dynamicCompile (element,notimeout,recursive) | This function can be used to compile "on" expressions that you add to elements that are created via Javascript. The first parameter is your element. The second parameter should always be true, and the third parameter should be true if you want to compile the children of your element - otherwise it should be false. |
| Appcelerator.Compiler.destroy (element,recursive) | Use this function to destroy the "on" expression for an element. This is typically used in cases where you are dynamically changing an element's "on" expression more than once. Each time you create a new "on" expression, you need to destroy the previous one. The first parameter is your element. The second parameter should be true if you want to destroy the children of your element - otherwise it should be false. |
| Appcelerator.Compiler.destroyContent (element) | Use this function to destroy all listeners on an element's children. The first parameter is a reference to the parent element. Unlike Appcelerator.Compiler.destroy, the parent element's listeners are left in tact. |
| Appcelerator.Compiler.getElementValue (element_id) | Use this function to get the value of an element depending on its type. |
| Appcelerator.Compiler.setElementValue (element, value) | Use this function to set the value of an element without being concerned with its type (saves one having to figure out if they should use innerHTML or value). The first parameter is a reference to the element. The second parameter is the new value to be set. |
| Appcelerator.Compiler.getElementChildren (element) | Use this function to get an element's childNodes array including only "ELEMENT_NODE" elements. The only parameter is a reference to the parent element. |
| Appcelerator.Compiler.copyAttributes (sourceElement, targetElement) | Use this function to copy attributes from sourceElement to targetElement. |
| Appcelerator.Compiler.getTagname (element) | Use this function to get the tag name of the element parameter, which is a reference to an element. |
| Appcelerator.Compiler.StateMachine.getActiveState (statemachine_id) | Use this function to get the current state of the statemachine with the id statemachine_id. |
| Appcelerator.Compiler.compileTemplate (templateText) | Create a template that uses Ruby-style string interpolation |
Cookie Functions
| Appcelerator.Util.Cookie.HasCookie (name) | Returns if a cookie exists for the passed in name. |
| Appcelerator.Util.Cookie.DeleteCookie (name1,name2) | Delete one or more cookies by name. |
| Appcelerator.Util.Cookie.GetCookie (name) | Get the value of a cookie by name. |
| Appcelerator.Util.Cookie.SetCookie (name,value) | Set the value of a cookie by name. |
Date Functions
| Appcelerator.Util.DateTime.getDuration (begin,end) | Return a formatted string that represents the duration between two Javascript Date objects |
| Appcelerator.Util.DateTime.friendlyDiff (begin,end,shortstr) | Return a formatted string that represents the difference between two dates. "shortstr" is boolean parameter that determines whether a shortened version of the string is display vs. a fully length string (e.g., a few seconds ago vs. a few secs ago). |
| Appcelerator.Util.DateTime.isToday (date) | Returns whether the date is today or not. |
| Appcelerator.Util.DateTime.isYesterday (date) | Returns whether the date is yesterday or not. |
| Appcelerator.Util.DateTime.getShortDateTime (date) | Returns a date with a short month (e.g, Apr) and a 12 hour time |
| Appcelerator.Util.DateTime.getShortMonthName (date) | Returns short month name (e.g, Apr) |
| Appcelerator.Util.DateTime.get12HourTime (date,seconds, milli) | Returns 12 hours time. The second and third parameters are boolean that determine whether seconds and milliseconds are included in the return value. |
| Appcelerator.Util.DateTime.parseRFC2822Date (date) | Return Javascript Date object when passed an RFC2822 date string |
| Appcelerator.Util.DateTime.format (date, format) |
The default build of Appcelerator includes the
Jel.Date library for parsing and formatting dates.
Example: Appcelerator.Util.DateTime.format(new Date(), "m/d/Y g:i:s A") returns "06/23/2008 5:39:35 PM" |
| Appcelerator.Util.DateTime.parse (date, format) |
Twin of Appcelerator.Util.DateTime.format, also from the
Jel.Date.
Example: Appcelerator.Util.DateTime.parse("2008/06/23", "Y/m/d").getTime() == new Date(2008, 5, 23).getTime() |
Validators and Decorators
You can also call validators and decorators via Javascript. Here’s a couple of examples:
Appcelerator.Validator.required(value)
Appcelerator.Decorator.required(element, valid, decId)
In the two examples above, we call the “required” decorator and validator. Each validator function requires the value of an element to be passed in. Each decorator function requires the element and whether the element is valid. You can also optional pass in the decorator ID for the decorator if applicable.