0

バックボーンコレクションがあります。フェッチします。サーバーはJSONで戻ってきます。コレクションに新しいデータを取り込むにはどうすればよいですか?これが私のコードです:

    var Todos = Backbone.Collection.extend({

       url: "server/todos-service.php", // this does what it is supposed to do so no worries!

       model: Todo,   

       initialize: function(attributes, options) {    
           // IN HERE WHAT DO I HAVE TO DO?
           // WHAT EVENT SHALL I BIND TO TO REACT THE DATA DELIVERY?
           // AND WHAT SHALL I DO NEXT TO POPULATE THE DAMN THINGS

           }
        });

    // CLIENT CODE
    new Todos().fetch();

誰かがこれがどのように行われるべきか教えてもらえますか?

乾杯

4

1 に答える 1

1

サーバーへのfetchajax呼び出しを開始します。データが返されると、データは自動的にコレクションに入れられ、コレクションはreset「データの取得が完了しました。今すぐ使用できます」のようなものを起動します。

通常、ビュー内からfetch()を呼び出し、ビューにresetイベントをリッスンさせます。リセットイベントがトリガーされると、ビューはコレクションをレンダリングします。

var Todo = Backbone.Model.extend({
    // my model
});

var TodoList = Backbone.Collection.extend({
     url: myurl
     model: Todo   
    // my collection (usually nothing in the initialize function) 
});


var AppView = Backbone.View.extend({
  initialize: function() {
    // instantiate the collection
    this.collection = new TodoList();

    //listen to the reset event on the collection. When the reset event trigger, call the render function.
    this.collection.on('reset', this.render, this);

    // get the data from the backend
    this.collection.fetch();
  },
  render: function() {
     // render the collection like a boss
  }
});

//instantiate the view 
var App = new AppView;

また、リソースとして、このチュートリアルはバックボーンの基本を理解するのに非常に役立つことがわかりましたhttp://net.tutsplus.com/sessions/build-a-contacts-manager-using-backbone-js/

于 2013-01-18T10:11:39.070 に答える