我正在尝试制作一个基本 Controller ,它提供其他 Controller 可以扩展的一些常见功能,但当我尝试扩展我的基本 Controller 时,我不知何故收到错误。
基础 Controller
define(function (require) {
var Marionette = require('marionette');
var BaseController = Marionette.Controller.extend({
initialize: function(options){
// Do stuff
},
// Other stuff
});
return new BaseController();
});
正在扩展的 Controller
define(function (require) {
var Marionette = require('marionette')
, BaseController = require('controllers/base_controller')
;
var MyController = BaseController.extend({
// Do stuff
});
return new MyController();
});
错误:
Uncaught TypeError: undefined is not a function mycontroller.js line 6
也许我无法扩展 Controller ?
最佳答案
您需要从基本 Controller 文件的 return 语句中删除 new
和 ()
return BaseController; // Remove the new && () here
使用 new BaseController()
调用会返回一个 BaseController
实例,而不是您尝试用来扩展
MyController 的构造函数与。
您还应该考虑从 MyController
文件中删除它们,然后在需要时创建新实例;
例如:
// controllers/my_controller.js
define(function (require) {
var Marionette = require('marionette')
, BaseController = require('controllers/base_controller');
var MyController = BaseController.extend({
// Do stuff
});
return MyController;
});
// someotherfile.js
define(function (require) {
var MyController = require('controllers/my_controller');
var myControllerInstance = new MyController();
});
这样,您的 Controller 模块将始终返回可用于再次扩展
的构造函数,而不是返回单个实例:
// controllers/my_awesome_controller.js
define(function (require) {
var MyController = require('controllers/my_controller');
var MyAwesomeController = new MyController.extend({
// Extend more awesomeness
});
return MyAwesomeController;
});
关于javascript - Marionette Js 扩展底座 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23902629/