我有一个可定制的导航树,可以嵌套 3 层。
模板:
<script type="text/x-handlebars" data-template-name="NavItemView">
<a {{bindAttr href="href" class="content.className"}}>{{content.name}}</a>
{{##if content.children}}
another collection here?
{{/if}}
</script>
<script type="text/x-handlebars">
{{collection App.NavItemsCollectionView contentBinding="App.navItemsController" tagName="ul"}}
{{view App.CreateLinkView id="new-link" placeholder="Name"}}
</script>
数据:
nav =[
{
"name": "Jethro Larson",
"children":[
{
"name":"Dashboard",
"href": "index.cfm"
}
]
},
{
"name":"Order Management",
"children":
[
{
"name":"OM Reports",
"children":
[
{
"name":"Status Updates",
"href":"index.cfm?blah"
}
]
}
]
}
];
js:
window.App = SC.Application.create();
App.NavItem = SC.Object.extend({
name: null,
href: '#',
});
App.navItemsController = SC.ArrayProxy.create({
content:[],
addMultiple: function(ar){
that = this;
$.each(ar,function(i,item){
that.pushObject(App.NavItem.create(item));
});
}
});
App.NavItemView = SC.View.extend({
tagName:'li'
,templateName: 'NavItemView'
});
App.NavItemsCollectionView = SC.CollectionView.extend({
itemViewClass: App.NavItemView
});
App.navItemsController.addMultiple(nav);
有没有办法嵌套集合,以便我可以将 dom 链接到数据结构?