对象不是函数'尝试用需求实例化木偶应用程序时出现错误
'Object is not a function' error when trying to instantiate Marionette Application with requirejs
这是我第一次尝试使用木偶。我试图实例化一个木偶应用程序作为一个需求模块。我一直在木偶。js wiki上使用木偶与Require js文章:-
https://github.com/marionettejs/backbone.marionette/wiki/Using-marionette-with-requirejs我认为我有我所有的shims,依赖关系和文件的顺序,因为我能够实例化视图,模型等在同一个地方没有错误,但我无法找出我的应用程序的问题。任何帮助或指导将非常感激!
这是我的index.html:-
<!DOCTYPE html>
<html>
<head>
<title>Marionette Structure and Require AMD Proto</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<div id="nav">
</div>
<div id="mainContent">
</div>
<script language="javascript">
// convenience function, because console.log throws errors and crashes IE
// this way you don't need to all logs to test in IE
function trace(msg){
try{
console.log(msg);
}catch(e){
// suppressed error
}
}
</script>
<script src="js/lib/require.js" data-main="app.js"></script>
</body>
</html>
我的app.js:-
require.config({
paths : {
backbone : 'js/lib/backbone',
underscore : 'js/lib/underscore',
jquery : 'js/lib/jquery',
marionette : 'js/lib/backbone.marionette'
},
shim : {
jquery : {
exports : 'jQuery'
},
underscore : {
exports : '_'
},
backbone : {
deps : ['jquery', 'underscore'],
exports : 'Backbone'
},
marionette : {
deps : ['jquery', 'underscore', 'backbone'],
exports : 'Marionette'
}
}
})
require(
["jquery",
"underscore",
"backbone",
"marionette",
"js/shell/shellapp"
],
function($, _, Backbone, Marionette, ShellApp) {
$(function() {
new ShellApp();
trace("ShellApp: "+ShellApp);
});
}
);
最后这里是我的shellapp.js:-
define( ["marionette"], function (Marionette) {
// set up the app instance
var ShellApp = new Marionette.Application();
// configuration, setting up regions, etc ...
ShellApp.addRegions({
nav: '#nav',
main: '#mainContent'
});
ShellApp.on('initialize:after', function(){
trace("initialize:after");
});
// export the app from this module
return ShellApp;
});
把所有这些放在一起,我得到"Uncaught TypeError: object is not a function "在app.js第42行
非常感谢所有走到这一步的人!
山姆我的回答太长了,不能评论!
导出的是一个构造对象,而不是构造函数本身:
var ShellApp = new Marionette.Application()
...
return ShellApp;
,这正是要导入的内容,因此您不需要创建另一个new
。
首先,我将ShellApp
重命名为shellApp
以表示实例,而不是构造函数(这是一种常见的命名约定)。我认为在教程中,他们打破了这个惯例,这是相当误导的:
MyApp = new Backbone.Marionette.Application();
(假设我没写错)。
我现在假设你只是传递这个Marionette.Application
的单个实例,在你的应用程序的生命周期。
在本教程中,它显示导出更新的 Marionette.Application
(与您所做的相同),但不显示在导入时实际使用它。在导入对象之后,您可以访问它的属性,例如:
shellApp.addInitializer(function(options){
// stuff
});
这里多。
相关文章:
- 使用Ampersand.js路由模块化应用程序
- 将broccoliJS与当前使用requireJS的模块化主干应用程序结合使用,我想完全放弃requireJS
- 如何通过osascript处理同一应用程序的两个实例
- 如何使用onDeviceReady设置AngularJS应用程序并初始化Cordova的功能
- Angularjs应用程序的多个实例
- 构建模块化谷歌地图应用程序
- 模块化与性能与性能与角度应用程序中的ng-include
- Rails应用程序中的每个javascript文件都需要.on('page:change',初始化)吗?
- 使用AsyncStorage实例化持久应用程序状态
- 在 Rails 上初始化应用程序 CSS 和 JavaScript 标签时,未显示 FullCalendar 标题工具栏
- 适用于 Android 应用程序的 Java 代码中的实例化异常
- 我们可以在角度应用程序中使用 requirejs 来仅管理/模块化应用程序的一部分吗?
- Ember-cli 创建/初始化应用程序.js两次
- 如何在视图中呈现部分 js 或访问在应用程序/资产中的 js.erb 文件中的控制器操作中初始化的实例变量/方法
- 解析nodejs中的反序列化应用程序/vnd.ms-excel
- 当同一个模板的两个实例在应用程序的多个地方使用时,Ember的奇怪行为
- 在Backbone/胸腔应用程序中实例化我的集合的位置
- 了解riot.js创建模块化应用程序的特性
- 如何实例化WinMD文件到Windows Store应用程序项目
- 对象不是函数'尝试用需求实例化木偶应用程序时出现错误