Provides methods to read values from annotated HTML configured for the Behavior class and its associated Filters.
new BehaviorAPI(element[, prefix]);
- element - (element) An element you wish to read.
- prefix - (string; optional) A prefix to all the properties; a namespace.
Examples of the HTML expressions evaluated are as follows (all of the following produce the same output*):
<tag data-behavior="Filter1 Filter2" data-filter1-options="{'opt1': 'foo', 'opt2': 'bar', 'selector': '.selector'}"> //prefered
<tag data-behavior="Filter1 Filter2" data-filter1-options="'opt1': 'foo', 'opt2': 'bar', 'selector': '.selector'"> //no braces on JSON
<tag data-behavior="Filter1 Filter2" data-filter1-options="{'opt1': 'foo', 'opt2': 'bar'}" data-filter1-selector=".selector">
<tag data-behavior="Filter1 Filter2" data-filter1-opt1='foo' data-filter1-opt2='false' data-filter1-selector=".selector">
The -options
value is parsed as JSON first (it's slightly more permissive in that you don't have to wrap it in {}
just for convenience). Values defined here are read as defined allowing you to express arrays, numbers, booleans, etc. Functions / callbacks are generally not used by Behavior.
If you attempt to read a value that isn't defined in this options object, the property name is attempted to be read from the property directly (e.g. data-behaviorname-prop
). This value is always a string unless you specify a type. If a type is specified the value is run through the JSON parser and validated against that type.
Note that filter names that contain characters other than A-Z, 0-9, or dash are stripped and what remains is case insensitive. Dots are turned to dashes. Further, camelCase properties are hyphenated to camel-case. So, for example, you would express the following:
<tag data-behavior="Foo.Bar" data-foo-bar-options="'someThing': true">
//and - note the hyphenation
<tag data-behavior="Foo.Bar" data-foo-bar-some-thing="true">
Gets a value for the specified name.
api.get(name[, name, name, name])
- name - (string) The name of the property you wish to retrieve. Pass more than one to get back multiple.
var api = new BehaviorAPI(target, 'foo');
api.get('bar'); //returns the value of data-foo-bar or null
api.get('bar', 'baz'); //returns {bar: 'value', baz: 'value'}
- (mixed) Values defined as strings will be returned as strings. Values defined in JSON will be returned as their type is evaluated. When you expect anything other than a string it's better to use getAs. When more than one name is specified you'll receive an object response with key/value pairs for the name/property values.
Gets a value for the specified name and runs it through JSON.decode and verifies that the value is parsed as the specified type (specifically a MooTools Type: String, Function, Array, Date, etc.).
api.getAs(Type, name[, defaultValue]);
- Type - (Type) A MooTools Type instance (a function) that the value, when run through JSON.decode, should return
- name - (string) The name of the value to read.
- defaultValue - (mixed) The value to set if there no value found.
var api = new BehaviorAPI(target, 'foo');
api.getAs(Number, 'some-number');
- (mixed) Either returns the value as the Type you specified, the default (if provided), or undefined.
Validates that an element has a value set for a given name. Throws an error if the value is not found.
api.require(name[, name, name]);
- name - (string) The name of the property you wish to require. Pass more than one if needed.
var api = new BehaviorAPI(target, 'foo');
api.require('foo'); //throws an error if data-foo-foo is not set
api.require('foo', 'bar'); //throws an error if data-foo-foo or data-foo-bar are not set
- object - the instance of BehaviorAPI.
Requires that an element has a value set for a given name that can be parsed into a given type (using JSON.decode). If a value is not present or does not parse to the specified Type an error is thrown.
api.requireAs(obj);
- obj - (object) a set of name/Type pairs to require.
api.requireAs({
option1: Number,
option2: Boolean
});
- object - the instance of BehaviorAPI.
Sets the default values. Note that setting defaults for required properties is not useful.
api.setDefault(name, value);
api.setDefault(obj);
- name - (string) The name of the property you wish to set.
- value - (mixed) The default value for the given name.
OR
- obj - (object) a set of name/value pairs to use if the element doesn't have values present.
api.setDefault('duration', 1000);
api.setDefault({
duration: 1000,
link: 'chain'
});
- object - the instance of BehaviorAPI.
The API class caches values read from the element to avoid the cost of DOM interaction. Once you read a value, it is never read from the element again. If you wish to refresh this to re-read the element properties, invoke this method. Note that default values are maintained.
api.refreshAPI();
- object - the instance of BehaviorAPI.