我正在重置集合时在我的主干应用程序中呈现视图
initialize: function(options) {
this.options = options;
this.model.get('projects').fetch({
data: {
organisation_id: this.model.get('id')
}
}, {reset:true});
this.model.get('projects').on('reset', this.render, this);
this.model.get('projects').on('add', this.addNewProject, this);
this.model.get('projects').on('sort', this.addProjects, this);
},
render: function() {
console.log(this.model.get('projects').state);
this.$el.html( this.template({
is_owner: this.options.is_owner,
className: this.options.className,
pagination: this.model.get('projects').state
}));
this.addProjects();
this.filter = new Pops.Views.OrganisationProjectsFilter({
el:this.$el.find('.div-organisation-filter-wrapper'),
model : this.model,
collection: this.collection
});
this.filter.render().el;
return this;
},
正如您所看到的,我运行了一个fetch并将我的集合重置为获取的数据。我的问题是在视图中我试图使用来自服务器的一些值,看起来它们是null,这是我的集合,
App.Collections.PaginatedProjects=
Backbone.PageableCollection.extend({
url: App.API_ROOT + "/projects/paginated",
// Initial pagination states
state: {
pageSize: 2,
sortKey: "name",
order: 1,
totalRecords:null
},
// You can remap the query parameters from `state` keys from
// the default to those your server supports
queryParams: {
totalPages: null,
totalRecords: null,
sortKey: "sort",
},
// get the state from Github's search API result
parseState: function (resp, queryParams, state, options) {
this.state.totalRecords = resp.total;
this.state.totalPages = resp.total / this.state.pageSize;
this.state.lastPage = this.state.totalPages;
},
// get the actual records
parseRecords: function (resp, options) {
return resp.data;
}
});
正如您所看到的,我正在运行解析函数以检索并设置为值,如果我控制我的集合,我可以看到正确的值,但是当我尝试在我的视图中使用它们时它们是null,我是否使用解析或重置错误或两者兼而有之?
答案 0 :(得分:0)
我认为这是因为当您呈现数据时尚未到达。试试这个:
this.model.get('projects').fetch({
success: function(model,response) {
var data = //some of your answer
}
});
答案 1 :(得分:0)
根据我的理解,你做了一个
console.log(this.model.get('projects').state);
并发现它打印正确,但随后在
中使用this.template({
is_owner: this.options.is_owner,
className: this.options.className,
pagination: this.model.get('projects').state
})
那么它没有出现?如果是这种情况,我认为这不是Backbone Collection中的问题。我认为这与您访问模板中的数据有关。
如果你使用的是把手,因为这个“状态”变量是一个json,
你可能正在使用
{{pagination.pageSize}}
在hbs文件中将其打印出来。我有一段不幸的记忆,以前不再支持它。尝试使用
{{pagination/pageSize}}
此外,如果此代码段位于{{#if}}或{{#each}}等内,根据句柄文档,您可能需要进入范围。使用
{{../pagination/pageSize}} or {{../../pagination/pageSize}}
前一段时间我遇到了这个问题,并做了很多搜索,发现了这个问题。希望它有所帮助