Unsorted Documentation
Terms
- Wired Object: An Autowire subclass which has been instantiated and attached to a node.
Getting Started Checklist
When you set up a new Autowire class, you need to ensure:
-
class ClassName {}
is declared -
ClassName.aw()
is called (usually immediately after the class definition) -
<node class="ClassName">
is declared Make sure: -
ClassName
matches in all three places - You've included
Autowire.js
and your own.js
file in the html page. - There are no javascript errors in the browser console
- Your node is being attached successfully:
- In
onAttach(){}
, writeconsole.log('I found the node!')
or something.
- In
Minor convenience things
-
myObj.innerHtml
is a shorthand formyObj.node.innerHtml
Extensions
- Your extension must override
onExtAttach()
.- This is called after
onCreate(...args)
and receives...args
-
this
refers to the instantiated autowire subclass this extension is attached to -
this.node
is available. - You can do literally whatever you want here. Though, beware of things that may be overridden during the
attach
step.
- This is called after
-
Autowire.expose(extensionKey, uninstantiatedClassObject)
: Make an extension available touse
- This is used by an extension developer to make their extension available.
Wiring Objects
These help you attach an Autowire class to a node
-
new MyModal(nodeObject, ...args)
: This is often the way to go if you have nested nodes to wire up, since you can passthis
to the nested node & it will have a reference to its root node. -
MyModal.aw('Accept Terms?')
will autowireMyModal
class to any nodes matching.MyModal
css selector and passesAccept Terms?
toonCreate()
andonAttach()
andonReady()
. -
MySubClass.autowire(querySelector=null, onPageLoad=true, ...args)
- if
querySelector
is not provided, then default of.MySubClass
is used. - pass
onPageLoad
asfalse
, and the wiring will happen immediately instead of waiting for page load to complete. - if
onPageLoad
istrue
, AND the page's loading is already complte, then wiring will be done immediately.
- if
Core static functions
-
MySubClass.use(extensionKey)
- Enable the named extension on the given class.
-
getObjectsFromName(className=null,parentObject=null)
.- Get all objects with the given class name, optionally only the ones who's nodes are children of the parent object's node
- if
className
is not provided, the called class's name is used.- Ex:
MyModal.getObjectsFromName()
is the same asAutowire.getObjectsFromName('MyModal')
- Ex:
-
getObjectFromNode(node)
: Gets the object wired to the node or null -
onPageLoad(func, thisArg, ...args)
: Will execute the given function when the page loads, binding thethisArg
& list of...args
. Executes immediately if the page is already loaded.
Core properties to use
-
this.node
. The node we're attached to- Shorthand:
this.n
- Shorthand:
-
this.name
. The class name
Core methods to override
-
onCreate
- Called before extensions are applied & listeners are setup. this.name & this.node are available
- Receives any args passed to the class constructor
-
onAttach
- Called after the node is completely ready. Click listeners are setup. Other nodes may not be setup yet.
-
onReady
- Called when there are no pending calls to autowire.
- This does not run if you create an autowire object directly (
new Autowire(node)
). - Not sure if it gets called after initial page load... I think so?
Core methods to call
-
q('.selector')
.- Shorthand for
this.node.querySelector('.selector')
(only searches children of this node)
- Shorthand for
-
qa('.selector')
.- Shorthand for
this.node.querySelectorAll('.selector')
- Shorthand for
-
getAny('ClassName')
:- Shorthand:
g('ClassName')
- Gets a Wired Object with the given classname
- Longhand:
Autowire.getObjectsFromName('ClassName')
- Shorthand:
-
getAnyList('ClassName')
- Shorthand:
ga('ClassName')
- Gets a list of Wired Objects with the given classname
- Shorthand:
-
fromTemplate('.selector')
-
fetch('/url',paramsObject, method="POST")
: Returns a response promise. You need toawait response.text()
or.json()
- Shorthands:
fetchText()
,fetchJson()
,ft()
&fj()
all with the same paramater list. Simply do:text = await ft('/whatever/');
- Shorthands:
-
bindTo(node)
- Bind
this
to each attribute-listener on node. - An
onclick
declared in html will have the called class asthis
, rather than the node.
- Bind
-
bindToAll(nodeList)
- same as
bindTo
except it takes a NodeList.
- same as