Ivia.js
A reactivity MVVM framework for jQuery with Vue-like interface.
Table of Contents
- Getting Started
- Binding Data (One-way-binding)
- DOM Events
- Two-Way Binding
- Show/Hide
- Find Element
- Wrap
- Methods / Watchers and Computed
- Events
- Setters
- Async/Sync
- Element Creation
Getting Started
Installation
Install by npm or yarn.
npm install jquery.ivia --saveyarn add jquery.ivia
Install by bower
bower install ivia --save
Download single file for browser.
CDN (Replace /0.1.1/
to latest version)
If you are using ES6 syntax to import module, you must inject jQuery or Zepto first.
;; Ivia$ = jQuery; // Or ZeptoIvia$ = Zepto; // Use _name to know which is in Ivia laterIvia$_name; // `jquery` or `zepto`
Make sure your environment has window
and document
object, Ivia does not support server environment now.
Create Ivia Instance
Ivia's interface is very similar to Vue, we can create an instance and add el
in option to select element.
Sometimes you may need to put your code before HTML <body>
, you can add domready
option.
Or use jQuery plugin pattern.
;
Create Custom Plugin
Sometimes you may need to handle a big project with many re-usable widgets, you can use Ivia to help you create jQuery plugins.
// Do not pass `el` option// This options will be default options. Ivia;
Now, use this plugin everywhere:
; // Or add custom options;
The new options will recursively merge with original one, so you can override any data. Use $options
later in Ivia instance to get your extra options.
this$optionsextraOptionsajaxUrl; // /post
Binding Data (One-way-binding)
Simple Binding
- API:
this.$bind(selector: string, dataPath: string, handler: string|Function)
- Handler:
function ($element: jQuery, value: mixed, oldValue: mixed, arrayControl: Object = null)
Ivia has a reactive data structure, you can bind an HTML element to a data value then if this data changed, Ivia will notice your handler to change DOM.
$bind()
method help us bin a value to element we selected, don't worry to double selected, Ivia will cache it.
the third argument tells Ivia to bind the attribute to this data. This is similar to Vue v-bind:data-number
directive.
An attribute name start with :
is a special control, currently we supports :text
, :html
and :value
, which will
auto use jquery .text()
, html()
or val()
to bind data, otherwise will use attr()
.
The rendered result will be:
sakura
Now you can change data:
spflowername = 'rose';
the text in <p>
will change to rose
, try this in playground
NOTE: All selector must under the root element which you set in
el:
option, Ivia will usethis.$find()
to find elements only contains in root element, if you have to control outside element, inject a custom element object.this.$bind(this.$('.my-outside-item'), 'flower')
Custom Handler
You will hope to do some extra action when data changed, just bind a callback.
this;
Array Operation
If data is an array and changed by push
, unshift
and splice
, there will be forth argument with control info,
it includes method
and args
to help you know what has been inserted.
var sp = el: '#app' data: items: 'a' 'b' 'c' { this; }; // Let's push a new itemspitems;
DOM Events
- API:
this.$on(selector: string, eventName: string, handler: Function, delegate: boolean = false)
- Handler:
function ($ele, event)
Use $on()
to bind DOM events to your data.
See example
If your elements are not always on page, your JS will add or remove them, you can add true
to forth argument to use
jQuery delegate event binding, then jQuery will help you watch your element.
this;
Two-Way Binding
- API:
this.$model(selector: string|Element, dataPath: string, delegate: boolean = false)
Two-way binding can only works for input
, textarea
and select
now.
See Example
Show/Hide
- API:
this.$show(selector: string|Element, dataPath: string, onShow: string|Function, onHide: string|Function)
Use $show()
to control an element show/hide depend on data, if value is true
, not empty string or bigger then 0
,
this element will be visible on page.
Zero One Two Negative One A B C
See Example
Hide element
Use $hide()
, the show/hide value will be reversed from $show()
.
Custom Show/Hide Handler
Use string
as jQuery method.
// Same as $ele.show()this; // Same as $ele.fadeIn()this; // Samse as $ele.slideDown()this;
If you only provide first handler, is will use as toggle handler.
this; // toggle show/hide this; // toggle fadeIn/fadeOut
Use callback:
this; // Use ES6 arrow functionthis;
See Example
Find Element
Use $find(selector)
to find all children element in the root element. Ivia will cache selector so you won't have multiple
select, if you want to refresh this select, add true
at second argument.
The $bind()
, $model
and $show()
method will cache target elements too, if you add some elements matches their selector,
you must call $find(selector, true)
to refresh cache. $on()
method can simply add true
at last argument to enable delegation.
Wrap
Simply use $wrap
so you don't need always type same selector:
this;
Only supports $bind
, $show
, $model
, $on
methods.
Methods / Watchers and Computed
methods, watches and computed is same as Vue.
var sp = el: '#app' data: firstName: 'john' lastName: 'williams' { this; this; this; } methods: { return text + text; } computed: { return this + ' ' + this; } watch: { // Do some manually update } ;
Also support $watch()
method:
this;
See Example and Vue: Computed amd Watchers
Events
Ivia events is also similar to Vue, the only different is that we change $on()
method to $listen
:
this; this; this;
Supported methods: $listen()
, $emit()
, $off()
, $once()
.
Setters
You must also use $set()
and $delete()
to operate some object in data that Ivia can add reactive observer.
this;this;
Async/Sync
nextTick
Ivia also supports $nextTick()
after all watchers updated the DOM:
thisfoo = 'new data'; this;
See Vue nextTick
Async DOM Operation
DOM operation is synchronous, Ivia provides an $async()
method to help us do something asynchronous and return Promise/A+ object.
this; // The below code will continue execute this; // These 2 tasks will not block each other.
Also you can use Promise.all()
or Promise.race()
, this example we convert jQuery ajax deferred object to Promise:
IviaPromiseall this this this;
Promise A/+
Ivia provides a Promise object in Ivia.Promise
, if browser supports native Promise, this object will be reference of native Promise.
If browser not support, Ivia will wrap jQuery.Deferred
and adapt it to fit Promise A/+ interface.
{ // Do something ;};
Element Creation
In the original way, we use $
to create element:
Ivia;
Ivia has a createElement()
method to help you generate DOM element:
Ivia;
We can add more children in the third argument.
Ivia;
Result:
Text Text Text
This is useful if we return Ajax JSON data to generate HTML.
TODO
- Component system
- Props
- State control
Credit
This project is inspired by Evan You's Vue.js and the reactive structure is heavily referenced of Vue.
License
Copyright (c) 2017, Simon Asika