Marionette を使い始めたばかりで、 David SulcによるMarionette - A Gentle Introductionを読んでフォローしています。非常に読みやすく、コンパニオン リポジトリを使用してサンプル アプリケーションContact Managerを構築する手順を簡単に理解できます。
しかし、私は以前に RequireJS を使用してプロジェクトをセットアップしたことがあり、その本のアイデアと概念をこのプロジェクトに翻訳して統合したいと考えていました。私は実際にはそこまで進んでおらず、オブジェクトにつながる AMD モジュールと組み合わせてマリオネット モジュールを使用することについて少し混乱していると思いundefined
ます。
より具体的に言うと、サンプル リポジトリのこのコミットのRequireJS バージョンであるとを挙げapp.js
てみましょう。listView.js
listController.js
app.js
/*global define*/
define([
'marionette'
], function ( Marionette ) {
'use strict';
var ContactManager = new Marionette.Application();
ContactManager.addRegions({
mainRegion : '#main-region'
});
ContactManager.on( 'initialize:after', function() {
ContactManager.ContactsApp.List.Controller.listContacts();
});
return ContactManager;
});
listView.js
/*global define*/
define([
'app',
'marionette',
'handlebars',
'text!templates/contact.hbs'
], function ( ContactManager, Marionette, Handlebars, contactTemplate ) {
'use strict';
var List = ContactManager.module( 'ContactsApp.List' );
List.Contact = Marionette.ItemView.extend({
tagName: 'li',
template : Handlebars.compile( contactTemplate ),
});
List.Contacts = Marionette.CollectionView.extend({
tagName: 'ul',
itemView: List.Contact
});
return List;
});
listController.js
/*global define*/
define([
'app'
], function ( ContactManager ) {
'use strict';
var List = ContactManager.module( 'ContactsApp.List');
List.Controller = {
listContacts: function() {
var contacts = ContactManager.request('contact:entities');
var contactsListView = new ContactManager.ContactsApp.List.Contacts({
collection: contacts
});
ContactManager.mainRegion.show( contactsListView );
}
};
return List.Controller;
});
したがって、次の行を参照するエラーが表示Uncaught TypeError: Cannot read property 'List' of undefined
されます。app.js:15
ContactManager.ContactsApp.List.Controller.listContacts();
これは、ContactsApp
モジュールがそうであることを意味し、undefined
これはまさに私が理解していないことです。
私の理解では、ContactsApp
モジュールとList
サブモジュールをContactManager
内側に、listView.js
またはlistController.js
(最初に呼び出された方)次の行で接続します。
ContactManager.module( 'ContactsApp.List' );
ContactsApp
内部で定義するべきではありませんapp.js
か?
これは、およびアプリケーションへのエントリ ポイントを含むmain.jsファイルです。require.config
require.config({
baseUrl: './scripts',
paths: {
jquery : '../bower_components/jquery/jquery',
underscore : '../bower_components/underscore/underscore',
backbone : '../bower_components/backbone/backbone',
marionette : '../bower_components/backbone.marionette/lib/backbone.marionette',
bootstrap : '../bower_components/sass-bootstrap/dist/js/bootstrap',
text : '../bower_components/requirejs-text/text',
handlebars : '../bower_components/handlebars/handlebars',
templates : '../templates'
},
shim: {
underscore : {
exports : '_'
},
backbone : {
deps : [ 'underscore', 'jquery' ],
exports : 'Backbone'
},
marionette : {
deps : [ 'backbone' ],
exports : 'Backbone.Marionette'
},
bootstrap : {
deps : [ 'jquery' ],
},
handlebars : {
exports : 'Handlebars'
}
},
deps : [ 'jquery', 'underscore' ]
});
require([
'app',
'bootstrap'
], function ( ContactManager ) {
'use strict';
ContactManager.start();
});