0

バックボーンで自分の作業を抽象化しようとしている<table>ので、「ベース ビュー」を拡張する「テーブル ビュー」を作成しました。したがって、新しいテーブルが必要な場合は、「テーブル ビュー」を拡張して、レンダリングしたい DOM 要素に「テーブル ビュー」を向けることができます。そのようです:

app.Views.quotesList = app.Views.table.extend({
    template: '#tmpl-table',


    initialize: function(){
        this.collection = new Backbone.Collection([
          {name: "Tim", age: 5},
          {name: "Ben", age: 26},
          {name: "Rob", age: 55}
        ]);
    },

    render: function() {
        var that = this;
        var template = Handlebars.compile( $(this.template).html() );
        $(this.el).html(template({id: 'quotes'}));

        this.table = this.$('#table-quotes');
        this.thead = this.table.find('thead tr');
        this.tbody = this.table.find('tbody');

        var template = Handlebars.compile( $('#table-heading').html() );
        that.thead.append(template(that.collection.first().toJSON()));

        var template = Handlebars.compile( $('#table-row').html() );
        that.collection.each(function(model){
            that.tbody.append(template(model.toJSON()));
        });
        return this;
    },

});

この一般的なコードのほとんどをテーブル ビューに移動したいと思います。

app.Views.table = app.Views.base.extend({

        initialize: function(){

        }

    });

では、「quotesList ビュー」からレンダリングする要素を「テーブル ビュー」に伝えるにはどうすればよいでしょうか。

4

1 に答える 1

0

ここであなたが何を求めているのかを誤解していない限りel、子ビューを作成するときに、テーブルビューから継承したとしても、渡すことができるはずです:

new app.Views.quotesList({ el: $('#foo') })

イニシャライザに渡す限り、親 (テーブル) ビューで定義されているrender()場合でも、呼び出し時またはそれを使用する他のものによって設定されます。render()

于 2013-01-08T09:39:14.840 に答える