Tripod.js

Two-way data-binding is sooo 2009.

Utility Functions

Tripod includes several utility functions it uses internally that you can also take advantage of through the Tripod.util object.

Tripod.util.isArray()

Method signature

Tripod.util.isArray(someVar)

Returns boolean true if the argument passed to it is an array.

Tripod.util.isNotBlank()

Method signature

Tripod.util.isNotBlank(someVar)

Returns boolean true if the argument passed to it is truthy or boolean false.

Tripod.util.arrayMap()

Method signature

Tripod.util.arrayMap(array, callbackFunction)

Iterates over the array passed and executes the passed function against every element of the array.

The array element is passed as the only argument to the function, and the function's return value is stored back in the array at the same index.

The resulting array is returned.

Example

var myArray = ['a', 'b', 'c', 'd'];
var upCaseIt = function(s) {
	return s.toUpperCase();
};
Tripod.util.arrayMap(myArray, upCaseIt); // returns ['A', 'B', 'C', 'D']

Tripod.util.trim()

Method signature

Tripod.util.trim(string)

A simple, fast string trim implementation.

Tripod.util.cloneObject()

Method signature

Tripod.util.cloneObject(object)

Performs a shallow copy of the passed object and returns the copy.

Tripod.util.toggleClass()

Method signature

Tripod.util.toggleClass(domNode, className, addOrRemove)

Adds or removes a CSS class based on the third argument. If the third argument is truthy, the class is added. Otherwise it is removed.

Classes will not be added twice, and no error will occur if toggleClass is asked to remove a class that isn't present.

Tripod.util.formatAsCurrency()

Method signature

Tripod.util.formatAsCurrency(numberOrString[, currencySymbol])

Given a number or string containing a number, returns a currency formatted string.

currencySymbol is optional and defaults to $

Examples

Tripod.util.formatAsCurrency(123456.7); // $123,456.70
Tripod.util.formatAsCurrency('123456'); // $123,456.00
Tripod.util.formatAsCurrency('hello123'); // $123.00
Tripod.util.formatAsCurrency(123.45, 'EUR'); // EUR123.45

Tripod.util.getNodeValue()

Method signature

Tripod.util.getNodeValue(domNode)

The method Tripod uses to retrieve the "value" of a DOM node.

See Node Properties Used for Data Binding.

Tripod.util.setNodeValue()

Method signature

Tripod.util.setNodeValue(domNode, value)

The method Tripod uses to set the "value" of a DOM node.

See Node Properties Used for Data Binding.

Tripod.util.getNodesByAttributeValue()

Method signature

Tripod.util.getNodesByAttributeValue(attributeName, attributeValue[, parentNode])

The method Tripod uses to get DOM nodes based on their binding attributes. You can pass at HTML attribute name and value to grab matching DOM nodes.

In modern browsers, Tripod.util.getNodesByAttributeValue returns a NodeList. In older versions of IE, this method returns an array. It's safest (and fastest) to iterate over the returned object using a standard for loop.

Tripod.util.processTemplate()

Method signature

Tripod.util.processTemplate(templateString, data)

A handlebars-ish templating engine. Tripod uses this for the template binding modifier.

Example

var templateString = "Hello, {name}!";
var data = { name: 'world' };
Tripod.util.processTemplate(templateString, data); // returns "Hello, world!"

Tripod.util.debounce

Method signature

Tripod.util.debounce(uniqueId, callbackFunction, timeoutDelay)

debounce is used to limit the rate at which a function can be called.

debounce is used internally by Tripod when triggering the set event. It's primary purpose is to prevent Tripod from triggering an excessive number of events and bogging down the browser as a user types into the input field.

Essentially, debounce introduces a small delay before executing the callback function. If debounce is called again before the delay timer runs out, the timer is reset and the waiting period begins again.

The timeoutDelay argument is optional and defaults to 250. timeoutDelay is in milliseconds.