문제
나는 ractive.js로 놀고있는 것에 대한 느낌을 얻으려고 노력하고 있습니다. 나는 공통적으로 공통적 인 예가 있으며, 내부 상태 논리가있는 뷰를 멋지게 유지하고 싶습니다. 튜토리얼을 통과 한 후에 이것은 밝은 무언가가 정말로 능력이있는 것처럼 보이지만, 나는 이것을 알아내는 데 어려움을 겪고 있습니다.
업데이트 : 나는 내가 가진 정확한 문제를 명확히하기 위해 내가 가진 첫 번째 대답에서 의견을 바탕으로 아래의 테스트 케이스를 개정했습니다. http://jsfiddle.net/e7mjm/1//A>
<div id="toc-view"></div>
<script id="ractive-toc" type="text/ractive">
<ul>
{{#chapters}}
<li class="{{type}}">
<span class="ordinal">{{ordinalize(ordinal)}}</span>
<a data-id="{{element_id}}">{{title}}</a>
{{#(sections.length > 0)}}
<a class="{{open ? 'expand open' : 'expand'}}" on-click="toggleSections"></a>
{{#open}}
<ul class="{{open ? 'sections open' : 'sections'}}">
{{#sections}}{{>section}}{{/sections}}
</ul>
{{/open}}
{{/()}}
</li>
{{/chapters}}
</ul>
<!-- {{>section}} -->
<li class="{{type}}">
<span class="ordinal">{{ordinalize(ordinal)}}</span>
<a data-id="{{element_id}}">{{title}}</a>
</li>
<!-- {{/section}} -->
</script>
.
ul { list-style: none; padding: 0; margin: 0}
ul.sections { padding-left: 20px; }
a.expand { color: red; }
a.expand:before { content: "+"; }
a.expand.open { color: blue; }
a.expand.open:before { content: "-"; }
.
및 다음 JavaScript가 모든 작업을 수행합니다.
data = [
{
id: "smith-about",
title: "About this book",
type: "front-matter"
},
{
id: "smith-preface",
title: "Preface",
type: "front-matter"
},
{
id: "smith-ch01",
title: "Intro to Biology",
ordinal: "1",
type: "chapter",
sections: [
{
id: "smith-ch01-s01",
title: "What is biology?",
ordinal: "1.1",
type: "section"
},
{
id: "smith-ch01-s02",
title: "What is a biologist?",
ordinal: "1.2",
type: "section"
},
{
id: "smith-ch01-s03",
title: "So you want to be a biologist?",
ordinal: "1.3",
type: "section"
}
]
},
{
id: "smith-ch02",
title: "Applied Biology",
ordinal: "2",
type: "chapter",
sections: [
{
id: "smith-ch02-s01",
title: "Biology in the lab",
ordinal: "2.1",
type: "section"
},
{
id: "smith-ch02-s02",
title: "Biology in the field",
ordinal: "2.2",
type: "section"
},
{
id: "smith-ch02-s03",
title: "Biology in the classroom",
ordinal: "2.3",
type: "section"
}
]
}
]
ractive = new Ractive({
el: 'toc-view',
template: '#ractive-toc',
data: {
chapters: data,
ordinalize: function(ordinal) {
return ordinal ? ordinal + "." : "▸";
}
}
});
ractive.on('toggleSections', function(event) {
event.context.open = !event.context.open;
this.update();
});
.
해결책
다음은 http://jsfiddle.net/e7mjm/a>
로 변경되었습니다.{{#open}}{{#sections}}{{>section}}{{/sections}}{{/open}}
.
를 열면 "주문시"렌더링됩니다.
event.context.open = !event.context.open;
this.update();
. 제휴하지 않습니다 StackOverflow