1

ワイン セラー アプリケーションの構築に関するバックボーン チュートリアルhttp://coenraets.org/blog/2011/12/backbone-js-wine-cellar-tutorial-part-1-getting-started/を読んでいます。チュートリアルの作成者は 1 つのポイントを明確に説明しておらず、ドキュメントからも理解できません。つまり、this.model.models以下のレンダー関数ビューに表示される の使用

window.WineListView = Backbone.View.extend({

    tagName:'ul',

    initialize:function () {
        this.model.bind("reset", this.render, this);
    },

    render:function (eventName) {
        _.each(this.model.models, function (wine) {
            $(this.el).append(new WineListItemView({model:wine}).render().el);
        }, this);
        return this;
    }

});

このビューのモデルは実際にはコレクションです

list:function () {
        this.wineList = new WineCollection();
        this.wineListView = new WineListView({model:this.wineList});

そして、コレクションは Wine をそのモデルとして宣言します

window.WineCollection = Backbone.Collection.extend({
    model:Wine,
    url:"../api/wines"
});

そのため、WineListView がインスタンス化されると、this.model実際にはワイン リスト コレクションになります。また、ドキュメントから、modelsコレクション内のモデルの配列へのアクセスを提供します

modelscollection.models 
Raw access to the JavaScript array of models inside of the collection. Usually you'll want to use get, at, or the Underscore methods to access model objects, but occasionally a direct reference to the array is desired.

this.modelがすでにワインのコレクションである場合(コレクションがビューでモデルとして宣言されているため)、なぜそれを行う必要があるのthis.model.modelsでしょうか? 本質的にコレクションを再び取得するには?

4

2 に答える 2

1

私の場合、それを機能させるには、次のことをしなければなりませんでした:

    _.each(this.model.models, function (foto) {
        console.log(foto.attributes);
        $(this.el).append(new App.view.foto.foto({model:foto.attributes}).render().el);
    }, this);

なぜ .attributes にアクセスするとうまくいくのかわかりません。変換が足りないのでしょうか?

DB から次の JSON 文字列をブートストラップしています。

[{
    "fid": 1,
    "imagen": "sample_colors.jpg",
    "width": "110",
    "height": "110",
    "dimension_id": 1,
    "seccion_id": 1,
    "estado": 0,
    "fecha": {
        "date": "2012-06-27 23:02:27",
        "timezone_type": 2,
        "timezone": "PDT"
    }
}, {
    "fid": 2,
    "imagen": "sample_colors.jpg",
    "width": "110",
    "height": "110",
    "dimension_id": 1,
    "seccion_id": 1,
    "estado": 1,
    "fecha": {
        "date": "2012-06-27 12:03:02",
        "timezone_type": 2,
        "timezone": "PDT"
    }
}, {
    "fid": 3,
    "imagen": "sample_colors.jpg",
    "width": "110",
    "height": "110",
    "dimension_id": 1,
    "seccion_id": 1,
    "estado": 2,
    "fecha": {
        "date": "2012-06-27 12:03:20",
        "timezone_type": 2,
        "timezone": "PDT"
    }
}]
于 2012-06-29T20:03:11.773 に答える
1

これは本質的に文体上の選択のようです。コード

_.each(this.model.models, function (wine) {
    $(this.el).append(...);
}, this);

コレクション内のモデルを単純に反復し、次と同等である必要があります。

this.model.each(function (wine) {
    $(this.el).append(...);
}, this);

2番目のバージョンの方が読みやすいと思っていたでしょうが、それぞれ独自のものです...

于 2012-04-17T20:39:00.987 に答える