未捕获的TypeError:MyView不是函数

时间:2015-09-21 04:24:55

标签: javascript backbone.js

我创建了my-view.js文件,如下所示:

(function (Backbone) {
  var MyView = Backbone.View.extend({
    className: 'card my-card',
    template: $('#my-template').html(),
    initialize: function(options) {
      this.render()
    },
    render: function() {
      var html = _.template(this.template);
      this.$el.html(html);
      return this;
    }
  });

  app.modules.MyView = MyView;
}(window.Backbone);

在app-controller.js中,我写了如下:

(function (Backbone) {
  var MyDayCardView = app.modules.MyView,  
  var AppController = Backbone.View.extend({
    initialize: function(){  
      var self = this;
    },
    appStart: function() {
      this.MyDayCardView = new MyDayCardView();
      self.$el.append(this.MyDayCardView.el);
    }
  });

  app.controllers.AppController = new AppController();    
}(window.Backbone);

然后我收到了错误:

  

未捕获的TypeError:MyView不是函数       Backbone.View.extend.appStart @ app-controller.js:

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您有一些语法问题,请参阅以下代码

var app={};
app.modules={};
app.controllers={};
// Add your javascript here
(function (Backbone) {
  var MyView = Backbone.View.extend({
    className: 'card my-card',
    template: $('#my-template').html(),
    initialize: function(options) {
      this.render()
    },
    render: function() {
      var html = _.template(this.template);
      this.$el.html(html);
      return this;
    }
  });

  app.modules.MyView = MyView;
}(window.Backbone));

(function (Backbone) {
  var MyDayCardView = app.modules.MyView,  
   AppController = Backbone.View.extend({
    initialize: function(){  
      var self = this;
    },
    appStart: function() {
      this.MyDayCardView = new MyDayCardView();
      self.$el.append(this.MyDayCardView.el);
    }
  });

  app.controllers.AppController = new AppController();    
}(window.Backbone));