js Code
function heredoc(fn) {
return fn.toString().replace(/^[^\/]+\/\*!?\s?/, "").
replace(/\*\/[^\/]+$/, "").trim().replace(/>\s*</g, "><")
}
var data = [{
id: "1",
title: "test1",
children: [{
id: "11",
title: "test11",
children: [{
id: "21",
title: "test21"
}]
}]
},
{
id: "2",
title: "test2"
}
]
var vm = avalon.define({
$id: "test",
treelist: []
})
setTimeout(function () {
vm.treelist = data;
}, 2000)
avalon.component("tree", {
template: heredoc(function () {
/*
<div>
<ul>
<li ms-for="(index, el) in @tree | get(0)" ms-class="["test", el.id == 1 && "test2"]" ms-click="@openSubTree(el)">
<a ms-attr="[{href:el.title},(el.children != null && el.children.length > 0) && {title:el.title}]">{{el.title}}</a>
<xmp ms-widget="{is:"tree-child", treeItem: el}"></xmp>
</li>
</ul>
</div>
*/
}),
defaults: {
tree: [],
openSubTree: function (el) {
el.open = !el.open
}
}
})
avalon.component("tree-child", {
template: heredoc(function () {
/*
<ul class="child-item">
<li ms-for="(index, el) in @treeItem.children | get(0)">
<span ms-click="@openSubTree(el)">{{el.title}}</span>
<xmp ms-widget="{is:"tree-child", treeItem: childitem}"></xmp>
</li>
</ul>
*/
}),
defaults: {
treeItem: {},
openSubTree: function (el) {
el.open = !el.open
}
}
})
html Code
<body ms-controller="test">
<xmp ms-widget="{is:"tree",tree:@treelist}"></xmp>
</body>