0

私はBackboneに比較的慣れていないので、サーバー側のコード間でデータをやり取りしようとしています。コレクションの設定があります:

var AppCollection = Backbone.Collection.extend({

    model: AppModel,

    url: 'data.php'

});

そして、私の見解では、初期化プルを持っています:

initialize: function(){
    items = this.collection.fetch();
    console.log(items);
}

オブジェクトを出力しitemsます。console.log(items.responseText)返されたJSONの内容を出力しようとしましたが、取得していundefinedます。

これが私のコンソールに表示されているものですconsole.log(items)

ここに画像の説明を入力してください

私の目標は、そのresponseTextを出力することだけです。何か案は?

4

1 に答える 1

6

As the backbone documentation says .fetch() returns a jQxhr object.

There are two possible ways to achieve, either you can write success and error callbacks to the fetch like this:

this.collection.fetch({
  success : function(collection, response) {
    // code here
  },

  error : function(collection, response) {
    // code here
  }
});

Or you can bind an event to reset method of the collection which is fired after the collection is successfully loaded.

this.collection.bind("reset", method_name);

You can access the collection on the method_name, as it will be executed after the fecth() ajax is executed. But I would suggest to use the first way as the second has its own other use cases.

于 2012-11-01T14:53:33.587 に答える