Simple Custom Attributes
This will allow you to define and use custom attributes in the DOM.
About
If you are used to using any modern front end frameworks, you are probably also used to doing things like:
// Angular// Rivets// Riot.js
This library makes custom attribute binding super easy without having to use some massive lame framework.
You can define your own with a simple attribute binding API.
Using The Library
npm install --save-dev simple-custom-attributes
- Require it in, and run the
register
method passing a dom element and a scope object.Then when you are done, youvar simpleCustomAttributes = ;simpleCustomAttributes;unregister
.simpleCustomAttributes;
This will register all custom attributes in the root element. No need to do it one at a time like I have done in the examples below.
Note: The this
scope of the method (if you are passing a method to the binding) is the object you registered.
Examples
on-swipe-right
attribute passing a method you want to call when the swipe right is complete.
Lets say you have a off-canvas menu and you wanted to make a swipe gesture close it. You would add a The DOM.
// Nav HTML HERE
The view
var simpleCustomAttributes = view = { // code to close the nav. // Note: `this` in here === the `view` } ; simpleCustomAttributes;
When the user swipes right, it will call that closeNav
function.
Lets say you had a element that has some fat paragraph of text in it and you wanted to clamp that to 3 lines.
The DOM.
400 lines of lorem ipsum.
The View.
var simpleCustomAttributes = model = linesToClampTheFatText : 3 ; simpleCustomAttributes;
After the registration is complete, that pile of text will be truncated to 3 lines.
Simple click handler.
The DOM.
The View.
var simpleCustomAttributes = view = { } ; simpleCustomAttributes;
Will call the yeahBuddy
method when the element is clicked.
Define your own custom attribute.
Each custom attribute must have the following methods.
{ // el === The element in question. // value === could be a function, a value, anything really. Depends on what you passed the binding. // NOTE: The scope of this closure is the bound object. so, `this` === the view you are binding // You add event listeners, do work etc here. } { // undo everything you did in the previous method. }
To include this custom attribute into the library, either make a pull request. OR. Call the addAttribute
method.
When you call addAttribute
you will pass a string corresponding with the attribute name and a object containing the above mentioned properties. (you can also use this to override existing attributes.)
If I wanted to define a on-input
handler for an input element.
Like so:
var simpleCustomAttributes = ; simpleCustomAttributes
Semi Baked in Attributes
Right now, we included the following into the library cause I use them all the time:
They are in the repo, but you will need to call addAttribute
on the instance to actually use them.
line-clamp='model.linesToClamp'
var customAttributes = ; customAttributes;
This text will be clamped to 4 lines with an ellipsis at the end.
on-mouse-in='someMethod'
var customAttributes = ; customAttributes;
on-mouse-out='someMethod'
var customAttributes = ; customAttributes;
on-swipe-left='someMethod'
var customAttributes = ; customAttributes;
on-swipe-right='someMethod'
var customAttributes = ; customAttributes;
on-click='someMethod'
var customAttributes = ; customAttributes;
on-event='{ event : method, event : method }'
var customAttributes = ; customAttributes;
on-click-and-hold
var customAttributes = ; customAttributes;
on-enter-viewport (NOTE!!!! THIS ONLY WORKS IN CHROME 51 BECAUSE IT USES INTERSECTION OBSERVERS)
var customAttributes = ; customAttributes;
// Note: SomeMethod will be called with the % the element is in the viewport.
on-exit-viewport (NOTE!!!! THIS ONLY WORKS IN CHROME 51 BECAUSE IT USES INTERSECTION OBSERVERS)
var customAttributes = ; customAttributes;
inner-html
var customAttributes = ; customAttributes;
on-progress (Used for video elements)
var customAttributes = ; customAttributes;
Let me know how it goes!!!