0

こんにちは、次のことをどのように行うかを理解するのに苦労しています。

レンダリングするバックボーン ビューがあり、そのビュー内の dom 要素に別のビューを追加したいと考えています。

これが私のコードです。

define([
  'jquery',
  'underscore',
  'backbone',
  'views/common/parent',
  'text!templates/currentAccounts.html',
  'text!templates/guidesTools.html'
], function($, _, Backbone, ParentView, mainTemplate, subTemplate){

var accountsView = ParentView.extend({
    render : function() {
        this.$el.html(mainTemplate);
    }
});

return new accountsView;

});

サブテンプレート (guidesTolls.html) をメイン テンプレート内の dom 要素にアタッチする必要があります。これを行う最良の方法は何ですか?

4

1 に答える 1

1

http://backbonejs.org/#examples-todosから例を学んでみてください

この部分には、2つのモデルと3つのビューがあります

  • モデル/コレクション
    • Todoモデル
    • TodoListコレクション
  • ビュー
    • Todoビュー
    • TodoListビュー
    • アプリビュー

アプリビューは、各TodoオブジェクトのTodoビューをレンダリングするTodoListビューに基づいてビューをレンダリングします。

あなたのケースはそれよりも単純です。具体的には、このようなことを実行して、サブビューを含むビューを作成できます

<div id="main">
</div>
<script type="text/template" id="main-template">
    <h3>main view</h3>
    <ul>
      <li><%= account.id %></li>
      <li><%= account.name %></li>
    <ul>
    <div id="guide-tools"></div>
</script>

Javascript:

var accountsView = ParentView.extend({
    el: $("#main")
    render : function() {
        var template = _.template($("#main-template").html());
        var guideTemplate = "Guide Tool Text";
        this.$el.html(template(this.model.toJSON()));
        this.$("#guide-tools").html(guideTemplate);  // will insert the guide page to the #main div
    }
})

注:特にサーバーサイドスクリプトを使用している場合は、htmlファイルからテキストを取得する方法がわかりません。

于 2013-02-26T09:48:13.017 に答える