【发布时间】:2015-12-29 20:07:52
【问题描述】:
我在我的 Backbone 集合上设置了一个属性 currentPage,但是我似乎无法专门记录它。
onRender: function () {
App.log('this.collection.currentPage onRender')
App.log(this.collection)
App.log(this.collection.accountID)
App.log(this.collection.currentPage)
App.log(this.collection.get('currentPage'))
}
我执行获取然后显示,并在 onRender 中生成以下控制台日志
其中this.collection.currentPage 和this.collection.get('currentPage') 是undefined,尽管我可以在this.collection 的日志输出中将currentPage 视为已定义的(“2”)属性。 this.collection.accountID 正在按照我期望的 currentPage 工作。
我做错了什么?
编辑:我在.fetch 的success 上设置属性,因为数据在响应标头中返回。像这样的,
getAccountSegments: function (accountID) {
var accountSegmentsCollection = new Entities.Collections.AccountSegmentCollection({
accountID: accountID
});
accountSegmentsCollection.fetch({
success: function (collection, response, options) {
collection.currentPage = options.xhr.getResponseHeader('X-Current-Page');
}
});
return accountSegmentsCollection;
},
下面有人说我不允许在集合上设置属性(除了通过构造函数,但那是在获取之前)。那么我还能如何将这个响应头数据放到我的视图中呢?
【问题讨论】:
-
您何时/何地设置属性..?也许你在渲染后设置它。我们怎么知道..?分享相关代码。
-
@TJ 查看我的编辑,我在
.fetch之后设置它。奇怪的是,我看到 currentPage 在 onRender 中正确记录了集合,但当我直接记录 currentPage 时却没有。 -
仍然不清楚
onRender和fetch的调用顺序。您何时/如何致电getAccountSegments和onRender..?你确定渲染发生在 fecth.. 之后吗?
标签: javascript backbone.js collections console.log