0

コレクションURLを使用してサービスコールを行っています。サービスはいくつかのjsonを返します。jsonの長さは13です(つまり、13行のデータ)。

ここで、this.Collection.fetch()は長さが13のjsonを返しますが、this.Collection.toJSON()は長さが12のjsonを返します。代わりに長さ13を返す必要があります。

コレクション解析では、応答は長さが13のjsonを返します。これは正しいです!

tableTemplateはテンプレートのオブジェクトです(テンプレートはHandlebars.jsを使用して実行されます)。

this.Collection.fetch({success:function(){

        console.log("Collection Fetch 2:");
        console.log(this.Collection.fetch());

        console.log("Collection toJSON: ");
        console.log(this.Collection.toJSON());
        console.log(this.Collection.toJSON().length);


        var markup = tableTemplate({List:self.importCollection.toJSON()});
         ...
          ...
    }
});
4

1 に答える 1

1

それがjavascript/backboneの非同期性です

このようにしてください:

this.collection.fetch();

//Fetch() is asynchronous call , when it completes fetching it triggers the **reset**   event so you need a event listener for **reset**

this.collection.on('reset',function(data){
console.log(data);                          // this will log the object
});

this.collection.on('reset',function(data){
console.log(JSON.stringify(data));        // This will log the JSON data  
});

またはあなたはこれを行うことができます:

this.collection.fetch({
success : function(){                      // called on completion of fetch()
console.log(data);       
console.log(JSON.stringify(data));  
}
});
于 2013-02-13T18:02:26.487 に答える