Backbone.Collection.reset()在IE8中抛出异常

时间:2012-12-06 14:32:00

标签: javascript internet-explorer backbone.js coffeescript marionette

每次,我在Backbone.Marionette.CollectionView中重置Backbone.Collection时,IE8都会抛出错误:

  

Function.prototype.apply:参数为null或未定义

同样的情况适用于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);
    }
  }
});

我正在使用简单的CollectionView,带有Handlebars模板:

  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);
  }
});

2 个答案:

答案 0 :(得分:3)

这是Marionette中的一个错误,使用null或未定义的.apply参数调用arguments

Marionette和它的依赖关系已经更新以解决这个问题,并且修复程序不是坐在他的dev分支中(参见此票证:https://github.com/marionettejs/backbone.marionette/issues/368)。

如果您需要立即修复,则需要从dev分支抓取牵线木偶版本:https://github.com/marionettejs/backbone.marionette/tree/dev/lib

如果您正在使用“核心”版本,请务必同时更新Wreqr和EventBinder。 BabySitter不需要任何更新。

答案 1 :(得分:2)

确保对象的所有没有逗号逗号:

ie8err: { something: 1, something: 2, } //Error in IE8 and below.

我不是肯定的,但它闻起来有点像。