骨干触发事件在两种情况下失去了上下文

时间:2014-07-01 17:38:36

标签: backbone.js backbone-events

我们有一个父视图和多个子视图,一种primary和另一种secondary。我们正在同时转换父视图和子视图,为此,我们通过'transition.event'在父级触发dispatch.trigger('transition.event', this),并通过

initialize: function(options) {
  if(options) {
    ... we place everything from options.* onto this.*
    // we can verify that the parameters exist coming in from options
    // This is listener A
    dispatch.on('transition.event', this.transition, this);
  }
  ...
  // This is listener B
  dispatch.on('transition.event', this.transition, this);
  this.render();
}

选项案例适用于类型设置为secondary的时候。

如果我们删除了侦听器A,则该事件不起作用,因为上下文丢失 如果我们删除了听众B,那么事件就行了,上下文当然不会丢失 如果我们将侦听器A更改为dispatch.on('secondaryTransition.event, this.transition, this);并将侦听器B保留原样,则可以正常工作。

为什么在将条件侦听器设置为骨干触发器时会丢失上下文和一些参数?

更完整的代码:

父视图:

makeChildViews: function(options){
  this.measurePassingToChildViewParameters = {...};
  // for each child 
  _.all(this.parentMeasureModel.get('beats').models, function(beat, index) {
    // create a Childview
      ...
        this.lineStatesUnrolling = [...];
        this.lineStatesRollup = [...];

    this.measurePassingToChildViewParameters.lineStatesUnrolling = this.lineStatesUnrolling;
    this.measurePassingToChildViewParameters.lineStatesRollup = this.lineStatesRollup;
    // we can verify that the parameters exist when we create the Child view
    new ChildView(this.measurePassingToChildViewParameters);
  }, this);
},

子视图:

transition: function() {
  // even though the parameters came in from the (options) in the init
  // they disappear when we have both listeners set
  console.warn(this);  //here this.lineStatesUnrolling is undefined
  for(i=0; i<this.transitionNumberOfPoints; i++){
      var x = this.transitionNumberOfPoints-i;
      this.BEAT.data([this.lineStatesUnrolling[i]])
          .transition()
          .delay(this.transitionDuration*i)
          .duration(this.transitionDuration)
          .ease('linear')
          .attr('d', this.pathFunction)
  }
},

错误:无法访问未定义的<0]

0 个答案:

没有答案