raziloBind - Alterers ES6 JS/HTML Binding Library
What is raziloBind?
ES6 JS/HTML binding library for creating dynamic web applications through HTML attribute binding. Made up of 4 libraries, puled in via a parent package that pulls in all required parts and configures as importable ES6 module 'RaziloBind'.
- razilobind-core (the main part), to traverse, detect and observe.
- razilobind-binder [injectables] (the actual binders), binding object properties to elements to do various things.
- razilobind-resolver [injectables] (to parse attribute data), resolving attribute data to things like strings, numbers, objects, methods etc.
- razilobind-alterer [injectables] (to change things), altering resolved data to something else without affecting the model.
This package razilobind-alterer, is injectable functionality that alters bound, resolved data before applying the data in the binded fashion. Think of it like formatting a timestamp to a date string, or inverting a property value.
What are Alterers?
Alterers are a way to change the end result of bound data that has been resolved. Simply, we create a bind (to do a thing), resolve the data in the bind (to know what to bind), then alter the resolved data if we wish. Alterers are the last point of that chain.
Alterers change the resolved data before using it in the binded element in the fashion it was intended to be used, such as trimming whitespace on strings, or formtatting dates. Substitue ??? for the binder you wish to alter the data on alter-*=""
, such as alter-text, alter-show... with the alterer/s specified in attribute value such as
Alterers are linked to the binder you wish to change, and they take in any resolvable data, see here for what types of resolvable data can be used.
<!-- single alterer which gets the alterer type from the bound foobar property of the model --> <!-- single alterer (as a string value) --> <!-- multiple alterers (as a strings or property) affects accumulate in order --> <!-- alterers with options (send in data to the alterer) -->
NOTE: We don't link arbitary functions to an alterer, if you bind a property or a method, it will use the result of this to look for the alterer to use! If you want custom alterers, then define this as correct alterers and inject them into the tool correctly, see how further down.
What Alterers are Available
trim Trims whitespace from string
Trim whitespace from start and end of resolved string.
- Accepts Type string
- Options none
- returns string
json JSON Stringify data
Stringifies various data types to a JSON readable string.
- Accepts Type all 'typeof' data types
- Options none
- returns string
prefix Add to start of string
Add a string to the start of a string, such as http:// infront of a web address.
- Accepts Type string
- Options string
- returns string
suffix Add to end of string
Add a string to the end of a string, such as .com to a TLD name.
- Accepts Type string
- Options string
- returns string
join Catenate strings into string
Catenate strings together into a single string.
- Accepts Type object of strings
- Options none
- returns string
date Format data to date string
Format various data types to a date string in a specified format. Ported from dateFormat https://github.com/felixge/node-dateformat by Steven Levithan <stevenlevithan.com>, please visit this package for details on format types.
- Accepts Type 'string', 'number', 'object', 'symbol'
- Options string
- returns string
not Boolean inversion of data
Return a boolean inversion of the resolved data.
- Accepts Type all 'typeof' data types
- Options none
- returns boolean
equal Return boolean if equal to
Checks if the bound resolved data is equal to option alterer value.
- Accepts Type All resolved types
- Options mixed
- returns boolean
identical Return boolean if identical to
Checks if the bound resolved data is identical (in value and type) to option alterer value.
- Accepts Type All resolved types
- Options mixed
- returns boolean
Making your own Alterers
There are two ways to add your own alterers to the system, by injecting them with the addAlterers() method bundled with razilobind, or if you have decided to import the core and have extended it, you may inject them along with all the other alterers in the same fashion.
First off you will need a new alterer, you can start off by taking an existing alterer and copying it, changing the necessary parts. Lets call this your-test.alterer.js...
/** * Test Alterer * A new test alterer * * Inherits * * properties: name, accepts * method: detect(name, resolved) { return bool } */ { super; thisname = 'your-test'; // this is the name you use in the html, it is how we detect if alterer should be used along with accepts below thisaccepts = 'string'; // as returned by 'typeof resolvedData'..... [] = any type, ['string'] = string only } /** * alter() * Changes resolved data based on options * @param mixed resolved The data to change * @param mixed options Any options sent in with the alterer * @return mixed Changed resolved data */ { // add Boom!!! to end of string return resolved + ' Boom!!!'; }
You can now import this into your project logic along with razilobind, injecting YourTestAlterer into razilobind by adding custom alterer...
var model = foo: 'foo' bar: 'bar'; var rb = ;rb;rb;
or if you have extended the core with your own class, you can add them as follows...
{ superoptions; // Inject injectables, pull in what you need! RaziloBindCoreDetectordefaultAlterers = TrimAlterer: RaziloBindTrimAlterer ...; RaziloBindCoreDetectordefaultBinders = ForBinder: RaziloBindForBinder ...; RaziloBindCoreDetectordefaultResolvers = BooleanResolver: RaziloBindBooleanResolver ...; // Inject custom injectables RaziloBindCoreDetectorcustomAlterers = YourTest: YourTestAlterer ...; }
...either way will inject custom alterers, should you wish to replace all default alterers with your own custom ones, substitute the default injectables with your custom ones. Default injectables will also be parsed first, followed by custom ones, you choose how to and what to inject.
Once your new alterer is injected, you should be able to use it like so (don't forget strings are in quotes, miss the quotes and you will be sending a property in!)