MarionetteJS CompositeView ListenTo undefined

时间:2013-01-26 05:13:11

标签: javascript backbone.js marionette backbone-views

我现在坚持。我收到以下错误:

TypeError: listenTo is undefined    
return listenTo.call(this, evtSource, events, _.bind(callback, context));

我不明白为什么会出现这种错误。我真的不知道listenTo期待什么。当我将Backbone.Marionette.CompositeView更改为BackboneView时,工作正常。有什么想法吗?

见下面的代码:

define([
        "jquery",
        "backbone",
        "marionette",
        ],
function($, Backbone, Marionette){

    var CompositeView = Backbone.Marionette.CompositeView.extend({
      // The DOM Element associated with this view
      el: ".example",
      // View constructor
      initialize: function() {
          // Calls the view's render method
          this.render();
      },
      // View Event Handlers
      events: {

      },

      // Renders the view's template to the UI
      render: function() {
          // Setting the view's template property using the Underscore template method
          //this.template = _.template('ddd', {});
          // Dynamically updates the UI with the view's template
          this.$el.html('123123123123123123123123');
          // Maintains chainability
          return this;
      }

    });

    // Returns the View class
    return CompositeView;
  }
);

2 个答案:

答案 0 :(得分:2)

listenToBackbone.Events的一项功能。 Backbone.View和随后的Marionette.View包括Backbone.Events功能。您将其称为全局定义;因此,您得到listenTo is undefined

您可以使用与myView.listenTo(model, 'change', myView.doSomething)类似的内容来调用它;或者如果当前上下文是您想要进行收听的视图实例,this.listenTo(object, 'eventName', this.doSomething)

答案 1 :(得分:2)