質問

私はRabtive.jsと遊んでいて、それが可能なものを感じることを試みています。私はかなり一般的な例を持っています、私がクリーンに追跡したいといういくつかの内部状態論理を持つビューです。チュートリアルを通過した後、これは何かractiveが本当に良いことのようですが、私はこれを考え出すのに苦労しています。


アップデート:私が持っている最初の答えのフィードバックに基づいて私のテストケースを下回って、私が持っていた正確な問題を明確にしました。ここでの完全な例を見ることができます。 http://jsfiddle.net/e7mjm/1/

最初に、私はロアトテンプレートを持っています:

<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>
.

これをフォーマットするための簡単なCSSスタイルがあります。

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をすべて作業するための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();
});
.

js fiddleを試すと、テンプレートが正しくレンダリングされますが、インタラクションの動作は正しくありません。 a.expandをクリックすると、そのセクションが開きますが、すべてのクラスも変更されます。他のa.expandアイコンは、クリックされたものだけでなく、

これは私が持っていた本当の問題です、ロキュアイベントのバインディングでは、ユーザーが対話している特定のデータオブジェクトのみに影響を与えるインタラクションを定義するための非常に良い方法はないようです。相互作用はすべてのデータに影響を与えるようです。

これを正しく照会する方法への洞察?

役に立ちましたか?

解決

これは http://jsfiddle.net/e7mjm/

です。

に変更
{{#open}}{{#sections}}{{>section}}{{/sections}}{{/open}}
.

今すぐ

を開くときに「オンデマンド」をレンダリングします。

event.contextを使用できます。ただ

event.context.open = !event.context.open;
this.update();
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top