18
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Backbone.jsで複数のモデルを扱うビューを作る場合

Last updated at Posted at 2012-09-18

初心者向け(自分)です。

Backbone.jsで複数のモデルを扱うビューを作る場合、以下のようにします。

ビュー定義
var HogeView = Backbone.View.extend({
  template:_.template($('#hoge-template').html()),
  initialize: function() {
    $(this.el).prepend(this.template({
      one: this.model.one,
      two: this.model.two
    }));
  }
});

テンプレート定義
<script type="text/template" id="hoge-template">
  <dl>
    <dt>1個目</td><dd><%= one %></dd>
    <dt>2個目</td><dd><%= two %></dd>
  </dl>
</script>
使う側
new HogeView({
  el: $('.hoge'),
  model: {
    one: one,
    two: two
  }
});

単純に複数のモデルをまとめたラッパーオブジェクトを挟むだけでした。
オブジェクトリテラルが書けるJavaScriptだからこそできる簡単な方法ですね。

18
17
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?