kist-view
Simple UI view. Inspired by Backbone.View.
Features:
- Subview managment: adding, getting and removing
- Optional DOM diff support for rendering
Install
npm install kist-view --save
Usage
Lilly button Honey content Roxie content
const View = ; const Shelby = View; var Sasha = Shelby; const shelby = ;const sasha = ;const roxie = el: '.roxie';
Render placeholder and view assign usage.
const View = ; const Shelby = View;
const View = ; const Shelby = View;
API
View.extend(options)
Returns: Object
View extends kist-klass
so it receives all it’s default properties. API usage for those properties is explanied on project page.
The only difference is list of properties and methods extend
method can receive.
initialize(options)
Type: Function
Initialization method which will should run after constructor
method.
el
Type: String|jQuery
UI element on which should view be initialized.
childrenEl
Type: Object
List of el
children elements.
Input like this:
'shelby': '.shelby' 'sasha': '.sasha'
Translates to output like this:
this$shelby = this$el;this$sasha = this$el;
events
Type: Object
List of el
events, delegated to children elements.
Input like this:
'click .shelby': 'method1' 'submit .sasha': 'method2' 'mouseleave .lilly': 'method3' { // Do something }
Translates to output like this:
this$el;this$el;this$el;this$el;
$(selector)
Type: Function
Returns: jQuery
Alias for this.$el.find(selector)
.
selector
Type: String|jQuery
String
is standard CSS selector.
setOptions(options)
Type: Function
Set instance options. It will set everything except el
, childrenEl
and events
.
options
Type: Object
render
Type: Function
Returns: View
Render method.
remove
Type: Function
Remove view.
setElement(element)
Type: Function
Sets or re-sets current UI element.
element
Type: String|jQuery
String
is standard CSS selector.
cacheChildrenEl(options)
Type: Function
Caches children elements.
options
Type: Object
See childrenEl
.
delegateEvents(events)
Type: Function
Delegates events.
events
Type: Object
See events
.
undelegateEvents
Type: Function
Undelegates events.
delegate(eventName, selector, listener)
Type: Function
Delegate single event.
eventName
Type: String
selector
Type: String
listener
Type: Function
undelegate(eventName, selector, listener)
Type: Function
Undelegate single event. For argument definition, see delegate
.
addSubview(view, key)
Adds subview to current view.
view
Type: View
Subview to add to current view.
key
Type: String|Number
Subview key so it can be easily identified in subview collection. If undefined,
view’s uid
property will be used.
getSubview(key)
Gets subview referenced by key.
key
Type: String|Number
Key which is used to reference subview.
removeSubviews
Removes all subviews from current view.
getRenderPlaceholder
Returns view’s placeholder element which will be used in resolving for
assignSubview
.
assignSubview(key)
Replaces view’s render placeholder with real content (returned when running
render
method).
If you’re using renderDiff
for content rendering, and view is instance of
DOM diff implementation, explicit
call for this method in parent view is unecessary—it will be called for every
subview which rendered it’s placeholder with getRenderPlaceholder
.
key
Type: String|Number
Key which is used to reference subview.
renderDiff(content, cb)
Renders DOM diff to current view’s element.
Available only for DOM diff implementation.
content
Type: String|Number|Element|jQuery
Content which should be diffed with current element and will be used to patch it.
cb
Type: Function
Called after current view’s element is updated by using
morphdom’s onElUpdated
hook. In most cases you won’t need
to use this callback, but it can be useful if DOM tree which needs to be
processed is large and you need to handle subview rendering after processing or
additional DOM manipulation.
fromTemplate
Type: Boolean
Should this view be fully constructed from template. Useful when you want to completely hold view representation inside template files (default view behavior is to have root element outside template).
Available only for DOM diff implementation.
DOM diff
Optionally you can use dom-diff
implementation of this module
to achieve performant rendering of your content. The difference in applying
template content is very subtle—instead of applying new content to DOM element
directly, you use convenient renderDiff
method.
Test
For local automated tests, run npm run test:automated:local
.
Browser support
Tested in IE9+ and all modern browsers.
License
MIT © Ivan Nikolić