2012-11-01 37 views
0

我是Backbone的新手,我试图在我的服务器端代码之间来回传递数据。我有一个采集设置:从Backbone Collection Fetch获取数据()

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)

enter image description here

我的目标只是输出的responseText。有任何想法吗?

回答

6

由于backbone documentation表示.fetch()返回jQxhr对象。

有两种可能的方式来实现,要么你可以写成功和错误回调到取像这样:

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

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

或者你可以绑定一个事件来复位的情况后烧制收集的方法收集已成功加载。

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

您可以访问在method_name的集合,如执行fecth()阿贾克斯后,它会被执行。但我会建议使用第一种方法,因为第二种方法有其他用例。

+0

如何在单独的文件中访问此jQxhr对象? – vini