Skip to content

Commit 34e8060

Browse files
Update client
1 parent 1816dbc commit 34e8060

File tree

12 files changed

+228
-321
lines changed

12 files changed

+228
-321
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# superviews.js
22

3-
Template engine for google [incremental-dom](http://google.github.io/incremental-dom)
3+
On the server `superviews.js` is used as a template engine for google's [incremental-dom](http://google.github.io/incremental-dom).
44

5-
Try it out [live in your browser](http://davidjamesstone.github.io/superviews.js/playground/index.html)</a>
5+
It can also now be used [in the browser](#client) to help build web applications based on [Custom Elements V1](https://www.w3.org/TR/custom-elements/)
6+
7+
Try it out [live in your browser](http://davidjamesstone.github.io/superviews.js/playground/index.html)
68

79
`npm install superviews.js --save`
810

911
## API
1012

1113
`tmplstr` (required) - The template string.
12-
`name` - The output function name (will be overridden with a <template> element).
13-
`argstr` - The output function arguments (will be overridden with a <template> element).
14+
`name` - The output function name (will be overridden with a `<template>` element).
15+
`argstr` - The output function arguments (will be overridden with a `<template>` element).
1416
`mode` - The output format. Can be one of ['es6', 'cjs', 'browser', 'amd'], if any other value is passed the function is exported as a variable with that name.
1517

1618
`superviews(tmplstr, name, argstr, mode)`
@@ -19,6 +21,12 @@ Try it out [live in your browser](http://davidjamesstone.github.io/superviews.js
1921

2022
`cat examples/test.html | superviews --mode=es6 --name=foo --argstr=bar > examples/test.js`
2123

24+
## Client
25+
26+
NEW! superviews can now be [used in the browser](docs/client.md).
27+
28+
Use it as a clientside library with a set of helpful classes and methods for building web applications based on the Web Components spec, specifically [Custom Elements V1](https://www.w3.org/TR/custom-elements/).
29+
2230
## Example
2331

2432
Create a file called `tmpl.html`

client/index.js

Lines changed: 22 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const superviews = (options, Base = window.HTMLElement) => class Superviews exte
3939
super()
4040

4141
const cache = {
42-
// initialState: initialState,
4342
options: options
4443
}
4544

@@ -58,38 +57,28 @@ const superviews = (options, Base = window.HTMLElement) => class Superviews exte
5857

5958
cache.render = render
6059

61-
/**
62-
* State
63-
*/
64-
// const store = new Store(initialState)
65-
66-
// store.on('update', function (currentState, prevState) {
67-
// render()
68-
// })
69-
70-
// cache.store = store
71-
// cache.initialFrozenState = store.get()
72-
7360
/**
7461
* Input props/attrs & validation
7562
*/
7663
const schema = options.schema
77-
if (schema) {
64+
if (schema && schema.properties) {
7865
const validate = validator(schema, validatorOptions)
7966
const props = schema.properties
80-
// const attrs = options.attributes
8167
const keys = Object.keys(props)
8268

69+
// For every key in the root schemas properties
70+
// set up an attribute or property on the element
8371
keys.forEach((key) => {
84-
let item = props[key]
85-
let isAttr = isSimple(item)
72+
const item = props[key]
73+
const isAttr = isSimple(item)
8674
let dflt
8775

8876
if ('default' in item) {
8977
dflt = item.default
9078
}
9179

9280
if (isAttr) {
81+
// Store primitive types as attributes and cast on `get`
9382
Object.defineProperty(this, key, {
9483
get () {
9584
return this.hasAttribute(key)
@@ -101,6 +90,7 @@ const superviews = (options, Base = window.HTMLElement) => class Superviews exte
10190
}
10291
})
10392
} else {
93+
// Store objects/arrays types as attributes and cast on `get`
10494
let val
10595

10696
Object.defineProperty(this, key, {
@@ -122,17 +112,11 @@ const superviews = (options, Base = window.HTMLElement) => class Superviews exte
122112
/**
123113
* Event Delegation
124114
*/
125-
126-
// Hold a map of bound handers to the original handler
127-
// const handlers = new Map()
128-
129-
// Initialise the delegator
130115
const del = delegator(this)
131116
this.on = del.on.bind(del)
132117
this.off = del.off.bind(del)
133118
cache.delegate = del
134119

135-
// cache.handlers = handlers
136120
cache.events = options.events
137121

138122
this.__superviews = cache
@@ -153,19 +137,21 @@ const superviews = (options, Base = window.HTMLElement) => class Superviews exte
153137
}
154138

155139
propertyChangedCallback (name, oldValue, newValue) {
156-
// Render on any change to observed property
140+
// Render on any change to observed properties
141+
// This can be overriden in a subclass.
142+
// To call this from the subclass use
143+
// super.propertyChangedCallback(name, oldValue, newValue)
157144
this.render()
158145
}
159146

160147
attributeChangedCallback (name, oldValue, newValue) {
161148
// Render on any change to observed attributes
149+
// This can be overriden in a subclass.
150+
// To call this from the subclass use
151+
// super.propertyChangedCallback(name, oldValue, newValue)
162152
this.render()
163153
}
164154

165-
// get state () {
166-
// return this.__superviews.store.get()
167-
// }
168-
169155
render (immediatley) {
170156
if (immediatley) {
171157
this.renderCallback()
@@ -174,55 +160,6 @@ const superviews = (options, Base = window.HTMLElement) => class Superviews exte
174160
}
175161
}
176162

177-
// on (eventType, selector, handler, useCapture) {
178-
// const del = this.__superviews.delegate
179-
// const handlers = this.__superviews.handlers
180-
181-
// // handler can be passed as
182-
// // the second or third argument
183-
// let bound
184-
// if (typeof selector === 'function') {
185-
// bound = selector.bind(this)
186-
// handlers.set(selector, bound)
187-
// selector = bound
188-
// } else {
189-
// bound = handler.bind(this)
190-
// handlers.set(handler, bound)
191-
// handler = bound
192-
// }
193-
194-
// del.on(eventType, selector, handler, useCapture)
195-
196-
// return this
197-
// }
198-
199-
// off (eventType, selector, handler, useCapture) {
200-
// const del = this.__superviews.delegate
201-
// const handlers = this.__superviews.handlers
202-
203-
// if (arguments.length === 0) {
204-
// // Remove all
205-
// handlers.clear()
206-
// } else {
207-
// // handler can be passed as
208-
// // the second or third argument
209-
// let bound
210-
// if (typeof selector === 'function') {
211-
// bound = handlers.get(selector)
212-
// handlers.delete(selector)
213-
// selector = bound
214-
// } else {
215-
// bound = handlers.get(handler)
216-
// handlers.delete(handler)
217-
// handler = bound
218-
// }
219-
// }
220-
221-
// del.off(eventType, selector, handler, useCapture)
222-
223-
// return this
224-
// }
225-
226163
emit (name, detail) {
227164
// Only emit registered events
228165
const events = this.__superviews.events
@@ -236,9 +173,10 @@ const superviews = (options, Base = window.HTMLElement) => class Superviews exte
236173
detail: detail
237174
})
238175

239-
// Call the DOM Level 1 handler if one exists
240-
if (this['on' + name]) {
241-
this['on' + name](event)
176+
// Call the DOM Level 1 handler if it exists
177+
const eventName = 'on' + name
178+
if (this[eventName]) {
179+
this[eventName](event)
242180
}
243181

244182
// Dispatch the event
@@ -276,6 +214,10 @@ const superviews = (options, Base = window.HTMLElement) => class Superviews exte
276214
static get schema () {
277215
return options.schema
278216
}
217+
218+
static get events () {
219+
return options.events
220+
}
279221
}
280222

281223
module.exports = superviews

docs/client.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ This can then be used in HTML like so:
2929

3030
`superviews.js` provides a thin wrapper around this pattern that includes events, event delegation and property and attribute validation.
3131

32-
The idea is that by describing what your components inputs (property/attributes) and outputs (events) are, you document the component and can also use this information to validate the state of the component.
32+
The idea is that by describing what your components inputs (property/attributes) and outputs (events) are, you document the component and can also use the information to validate the state of the component.
3333

3434
JSON Schema is used to describe the properties and attributes.
3535

@@ -42,11 +42,14 @@ All the examples are CJS/browserify.
4242
Say you want to build a Todo List Web Component.
4343

4444
It's to have a `theme` that can be either `light` or `dark` but defaults to `dark`.
45-
It also needs to be supplied a list of items called `todos`. Both these inputs are required. The list of `todo` items should be an array of objects with a required integer `id`, required `text` string and a boolean flag to indicate if the task `isCompleted`.
45+
It also needs to be supplied a list of items called `todos`.
46+
Both these inputs are required.
47+
48+
The list of `todo` items should be an array of objects with a required integer `id`, required `text` string and a boolean flag to indicate if the task `isCompleted`.
4649

4750
A third optional input is a `title` for the todo list and has a max length of 10.
4851

49-
The Todos component should emit 3 events - `change`, `add` and `remove`.
52+
The Todos component can emit 3 events - `change`, `add` and `remove`.
5053

5154
Using `superviews.js`, it looks like this:
5255

@@ -110,7 +113,7 @@ class Todos extends superviews(options) {
110113
window.customElements.define('x-todos', Todos)
111114
```
112115

113-
This could then be used like:
116+
This can then be used in HTML like this:
114117

115118
```html
116119
<x-todos theme="light" title="My Todos">
@@ -253,4 +256,4 @@ You can use it via `superviews.js` if you like:
253256
require('superviews.js/dre')
254257
```
255258

256-
For more info see the [examples](../examples) folder or open [todos](../examples/client/x-todos/index.html) or [widget](../examples/client/x-widget/test.html)
259+
For more info see the [examples](../examples) folder or open [todos](../examples/client/x-todos/test.html) or [widget](../examples/client/x-widget/test.html)

0 commit comments

Comments
 (0)