3

私はそのようなバックボーンコレクションを持っています:

var ThreadCollection = Backbone.Collection.extend({
  url: '/api/rest/thread/getList'
});
var myCollection = new ThreadCollection();

次に、データオブジェクトを使用してサーバーからフェッチし、クエリパラメーターを追加します(この場合、'/ api / rest / thread / getList?userId = 487343'になります)

myCollection.fetch({
  data: {
    userId: 487343
  }
})

userId(groupId、orgIdなど)の代わりに使用したい他のパラメーターがありますが、理想的には初期化時にデータパラメーターを定義し、それ以降は指定せずにfetch()を実行できるようにします。このようなもの:

var myCollection = new ThreadCollection({
  data: {
    userId: 487343
  }
});

myCollection.fetch()

しかし、それは機能しません。これを行う方法があるかどうか誰かが知っていますか?ありがとう!

4

1 に答える 1

6

1つの方法は、コレクションにカスタムメソッドを定義して、オーバーライド可能なデフォルトでfetchスーパーメソッドを呼び出すことです。fetch

var ThreadCollection = Backbone.Collection.extend({
    url: '/api/rest/thread/getList',
    fetch: function(options) {
        return Backbone.Collection.prototype.fetch.call(this, _.extend({
            data: {
                userId: 48743
            }
        }, options));
    }
});

var myCollection = new ThreadCollection();

myCollection.fetch();
于 2013-02-21T18:51:52.940 に答える