每次,我在 Backbone.Marionette.CollectionView 中重置 Backbone.Collection,IE8 都会抛出错误:
Function.prototype.apply:参数为空或未定义
同样的情况也适用于 emptyView。
我的 CoffeeScript 代码(以下 js 版本):
Collection = Backbone.Collection.extend
url : '/videos.json'
baseModels: []
initialize : ->
GIS.VL.vent.on 'specialization:selected', @onSpecializationSelected, @
@on 'reset', @onFirstSync, @
onFirstSync : (a)->
@off 'reset', @onFirstSync, @
@baseModels = @models
console.log @models
onSpecializationSelected : (model)->
if model.id < 0
@reset @baseModels
else
filteredItems = _.filter @baseModels, (item)->
specializations = _.map item.get('specializations'), (specialization) ->
specialization.id
-1 < _.lastIndexOf specializations, model.id
@reset filteredItems
Javascript版本:
var Collection;
Collection = Backbone.Collection.extend({
url: '/videos.json',
baseModels: [],
initialize: function() {
GIS.VL.vent.on('specialization:selected', this.onSpecializationSelected, this);
return this.on('reset', this.onFirstSync, this);
},
onFirstSync: function(a) {
this.off('reset', this.onFirstSync, this);
this.baseModels = this.models;
return console.log(this.models);
},
onSpecializationSelected: function(model) {
var filteredItems;
if (model.id < 0) {
return this.reset(this.baseModels);
} else {
filteredItems = _.filter(this.baseModels, function(item) {
var specializations;
specializations = _.map(item.get('specializations'), function(specialization) {
return specialization.id;
});
return -1 < _.lastIndexOf(specializations, model.id);
});
return this.reset(filteredItems);
}
}
});
我正在使用带有 Handlebars 模板的最简单的 CollectionView:
VideosView = Backbone.Marionette.CollectionView.extend
tagName : 'div'
itemView : VideoView
collection: new Collection
config:
itemsPerPage: 3
initialize: (options)->
console.log 'initialize'
# override defaults:
@config = _.extend @config, options
Javascript 版本如下:
var VideosView;
VideosView = Backbone.Marionette.CollectionView.extend({
tagName: 'div',
itemView: VideoView,
collection: new Collection,
config: {
itemsPerPage: 3
},
initialize: function(options) {
console.log('initialize');
return this.config = _.extend(this.config, options);
}
});