Skip to content

Commit 023213e

Browse files
committed
- add template to support recursive views
- prevent internal view from overwriting options provided by ldview logic - bump version
1 parent 76bba7a commit 023213e

File tree

13 files changed

+379
-26
lines changed

13 files changed

+379
-26
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Logs
22

3+
## v0.2.2
4+
5+
- add `template` to support recursive views
6+
- prevent internal view from overwriting options provided by ldview logic
7+
8+
39
## v0.2.1
410

511
- support refering to view root with name `@`.

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ Or, let ldView do it for you with `view` option:
9393
name: (.node.textContent = data.name)
9494
author: (.node.textContent = data.author)
9595

96+
While you can use the same options in this view config as the ldview constructor, following fields will be overwritten by ldview:
97+
98+
- `initRender`
99+
- `root`
100+
- `baseViews`
101+
- `ctx`
102+
- `ctxs`
103+
96104

97105
After initialization, You probably will want to update some elements instead of updating every node. Just pass target names into render function:
98106

@@ -184,6 +192,7 @@ Basically `Scope` and `Prefix` are mutual exclusive; with `scope` you don't have
184192
* `global` - set true to use `pd` and `pd-each` for access nodes globally beyond ld-scope. default false.
185193
* `ctx` - default data accessible with in handler functions. can be set later with `setContext` api.
186194
- `context` is used as `ctx` before `0.1.0`, and it's now `ctx`.
195+
* `template` - template DOM for replacing root content. It's for supporting recursive views.
187196
* `ctxs` and `baseViews` - these config are used internally. Don't use this unless you know what's your doing.
188197

189198
## API
@@ -236,6 +245,31 @@ When handlers for each ld node is called, it contains following parameters:
236245
- `views[0]` is always the current view. larger number gets ancestor views.
237246

238247

248+
## Recursive Views and Template
249+
250+
It's possible to define view recursively - simply refer a view config in itself:
251+
252+
(cfg = {}) <<< handler: someDirective: {
253+
list: -> ...
254+
view: cfg
255+
}
256+
257+
However, this requires a recursively defined DOM, which is only possible with `template` option:
258+
259+
div(data-name="template"): ...
260+
script(type="text/livescript").
261+
new ldview (cfg = {}) <<< {
262+
template: document.querySelector('[data-name=template]')
263+
handler: someDirective: {
264+
list: ({ctx}) -> ...
265+
view: cfg
266+
}
267+
}
268+
269+
In this case, the div named `template` will be cloned, attached and used as inner DOM of this view, recursively applied according to the return content of `list`. For a working example, check `web/src/pug/recurse/index.ls`.
270+
271+
272+
239273
## License
240274

241275
MIT

dist/index.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
this.id = "ld-" + Math.random().toString(36).substring(2);
3838
this.root.setAttribute("ld-scope-" + this.id, '');
3939
}
40+
if (this.template = opt.template) {
41+
this.root.textContent = '';
42+
this.root.appendChild(this.template.cloneNode(true));
43+
}
4044
this.update();
4145
names = {};
4246
for (i$ = 0, len$ = (ref$ = [(fn$.call(this))].concat([(fn1$.call(this))], [(fn2$.call(this))], [(fn3$.call(this))], [(fn4$.call(this))], (fn5$.call(this)).map(fn6$))).length; i$ < len$; ++i$) {
@@ -324,17 +328,11 @@
324328
if (b) {
325329
if (b.view) {
326330
init = function(arg$){
327-
var node, local, data, ctx, ctxs, views;
331+
var node, local, data, ctx, ctxs, views, ref$;
328332
node = arg$.node, local = arg$.local, data = arg$.data, ctx = arg$.ctx, ctxs = arg$.ctxs, views = arg$.views;
329-
return local._view = new ldview(import$({
330-
initRender: false,
331-
root: node,
332-
baseViews: views,
333-
ctx: data,
334-
ctxs: ctxs
335-
? [ctx].concat(ctxs)
336-
: [ctx]
337-
}, b.view));
333+
return local._view = new ldview((ref$ = import$({}, b.view), ref$.initRender = false, ref$.root = node, ref$.baseViews = views, ref$.ctx = data, ref$.ctxs = ctxs
334+
? [ctx].concat(ctxs)
335+
: [ctx], ref$));
338336
};
339337
handler = function(arg$){
340338
var local, data;

dist/index.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"license": "MIT",
55
"main": "dist/ldview.min.js",
66
"description": "view template micro framework",
7-
"version": "0.2.1",
7+
"version": "0.2.2",
88
"files": [
99
"dist/**/*"
1010
],

src/ldview.ls

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ ldview = (opt = {}) ->
2626
@id = "ld-#{Math.random!toString(36)substring(2)}"
2727
# ld-scope-${id} is used to identify a ldview object, for code of excluding scoped object
2828
@root.setAttribute "ld-scope-#{@id}", ''
29+
if (@template = opt.template) =>
30+
@root.textContent = ''
31+
@root.appendChild @template.cloneNode(true)
2932

3033
@update!
3134

@@ -168,10 +171,10 @@ ldview.prototype = Object.create(Object.prototype) <<< do
168171
if b =>
169172
if b.view =>
170173
init = ({node,local,data,ctx,ctxs,views}) ->
171-
local._view = new ldview({
174+
local._view = new ldview({} <<< b.view <<< {
172175
init-render: false, root: node, base-views: views
173176
ctx: data, ctxs: (if ctxs => [ctx] ++ ctxs else [ctx])
174-
} <<< b.view)
177+
})
175178
handler = ({local,data}) -> local._view.setCtx(data); local._view.render!
176179
else
177180
init = b.init or null

web/.view/recurse/index.js

Lines changed: 197 additions & 0 deletions
Large diffs are not rendered by default.

web/src/pug/recurse/index.ls

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
view = new ldview do
2+
root: document.body
3+
handler: template: (->), root: (->)
4+
template = view.get('template')
5+
template.removeAttribute \ld-scope
6+
template.parentNode.removeChild template
7+
root = view.get('root')
8+
9+
data = {name: 1, child: [
10+
{name: "1.1"}
11+
{name: "1.2", child: [
12+
{name: "2.1", child: [
13+
{name: "3.1"}
14+
{name: "3.2"}
15+
{name: "3.3"}
16+
]}
17+
{name: "2.2"}
18+
{name: "2.3"}
19+
]}
20+
{name: "1.3"}
21+
]}
22+
23+
24+
rview = new ldview (cfg = {}) <<< do
25+
ctx: data, root: root
26+
template: template
27+
text: name: ({ctx}) -> ctx.name
28+
handler: item:
29+
list: ({ctx}) -> ctx.child or []
30+
view: cfg

web/src/pug/recurse/index.pug

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
doctype html
2+
include @/bootstrap.ldui/dist/index.pug
3+
include @static/assets/lib/ldview/dev/index.pug
4+
html
5+
head
6+
+css("/assets/lib/bootstrap/main/css/bootstrap.min.css")
7+
+css("/assets/lib/bootstrap.ldui/main/bootstrap.ldui.min.css")
8+
body
9+
.w-1024.rwd.mx-auto.typeset.heading-contrast.my-4
10+
h3 Recursive Views Test
11+
div(ld-scope,ld="template")
12+
.px-4
13+
div(ld="name")
14+
div: div(ld-each="item")
15+
div(ld="root")
16+
+script("/assets/lib/@loadingio/ldquery/main/ldq.min.js")
17+
+script("/assets/lib/ldview/dev/index.js")
18+
script: include:lsc index.ls

0 commit comments

Comments
 (0)