1

特定のコレクションを取得できるシンプルなアプリを実装しようとしていobject_idます。
サーバーからのGET responseは次のようになります。

[
    {object_id: 1, text: "msg1"}, 
    {object_id: 1, text: "msg2"}, 
    {object_id: 1, text: "msg3"},
    .......
]

私の目標は
、ユーザーが を選択したときにコレクションをレンダリングすることobject_idです。

私のコードの出発点は次のとおりです。

this.options = {object_id: 1};
myView = new NeView(_.extend( {el:this.$("#myView")} , this.options));

私の質問は次のとおりです:
* 最善の方法は何ですか:
1) を設定してobject_id valueMyModel2
) fetchMyCollection でトリガーし、
3) ?*でrender関数をmyView
トリガーするか、私の目標をアクティブにしますか?


PS:

私の基本的なコードは次のようになります。

// My View
define([
    "js/collections/myCollection",
    "js/models/myFeed"
], function (myCollection, MyModel) {
    var MyView = Backbone.View.extend({

        initialize: function () {

            var myModel = new MyModel();

            _.bindAll(this, "render");

            myModel.set({
                object_id: this.options.object_id
            }); // here I get an error: Uncaught TypeError: Object function (){a.apply(this,arguments)} has no method 'set'
        }
    });

    return MyView;
});

// MyCollection
define([
    "js/models/myModel"
], function (MyModel) {

    var MyCollection = Backbone.Collection.extend({

        url: function () {
            return "http://localhost/movies/" + myModel.get("object_id");
        }
    });

    return new MyCollection
});

//MyModel
define([
], function () {

    var MyModel = Backbone.Model.extend({
    });

    return MyModel
});
4

1 に答える 1

4

Backbone の内部構造に関する基本的な理解には、根本的な問題ではないにしても、いくつかの問題があります。

まず、デフォルトのモデル idAttribute を定義します。これは、コレクションでモデルを検索するキーを識別するものです

//MyModel
define([
], function () {

    var MyModel = Backbone.MyModel.extend({
        idAttribute: 'object_id'
    });

    return MyModel
});

コレクションでは、定義した方法で URL を定義する必要はありません。変更する必要があるのは 2 つあります。1 つ目はコレクションのデフォルト モデルを定義すること、2 つ目はコレクションのベース URL をそのまま使用することです。コレクション

// MyCollection
define([
    "js/models/myModel"
], function (MyModel) {

    var MyCollection = Backbone.MyCollection.extend({
        model: MyModel, // add this
        url: function () {
            return "http://localhost/movies
        }
    });

    return MyCollection // don't create a new collection, just return the object
});

そして、あなたの見解はこれらの線に沿ったものになる可能性がありますが、確かにこの実装方法に限定されません

// My View
define([
    "js/collections/myCollection",
    "js/models/myFeed"
], function (MyCollection, MyModel) {
    var MyView = Backbone.View.extend({

        tagName: 'ul',

        initialize: function () {
            this.collection = new MyCollection();
            this.collection.on('add', this.onAddOne, this);
            this.collection.on('reset', this.onAddAll, this);
        },

        onAddAll: function (collection, options)
        {
            collection.each(function (model, index) {
                that.onAddOne(model, collection);
            });
        },

        onAddOne: function (model, collection, options)
        {
            // render out an individual model here, either using another Backbone view or plain text
            this.$el.append('<li>' + model.get('text') + '</li>');
        }

    });

    return MyView;
});

気を取り直して、一歩一歩進んでください

Backbone.js github wiki: https://github.com/documentcloud/backbone/wiki/Tutorials%2C-blog-posts-and-example-sitesのチュートリアルの完全なリストを詳しく調べることを強くお勧めします。 .. require.js の複雑さを追加する前に、Backbone の基本を理解しようとする

于 2012-05-17T09:21:29.540 に答える