Ich möchte Zugriff auf meine Main-App-Instanz von Hauptcontroller erhalten, um neue Ansicht zu rendern. Was ich will, in Controller/main.js Code tun, kommentiert, wie ich das verstehe, ist circ Abhängigkeit in erfordern js, aber ich verstehe nicht, wie dasMarionette js Zugriff auf die App vom Controller mit requirs
file structure
- controllers/main.js
- models/
- templates/
- views/
- app.js
- main.js
- router.js
main.js beheben
require.config({...})
require(['app'], function(app) {
app.initialize().start();
});
app.js
define(['jquery', 'underscore', 'backbone', 'marionette', 'router','controllers/main'],
function($, _, Backbone, Mn, Router, MainController) {
let app = null;
const App = Mn.Application.extend({
region: '#app',
initialize(options) {
this.router = options.router;
},
onBeforeStart() {
console.log('before start');
},
onStart() {
Backbone.history.start();
}
});
return {
get instance() {
return app;
},
initialize() {
if (!app) {
const controller = new MainController();
const router = new Router({controller});
app = new App({router});
}
return this;
},
start() {
this.instance.start();
},
}
});
router.js
define([
'jquery',
'underscore',
'marionette',
], function($, _, Mn) {
return Mn.AppRouter.extend({
initialize(options) {
this.controller = options.controller;
},
appRoutes: {
'': 'index',
'profile': 'profile',
'*notFound': 'notFound'
},
});
});
Controller/main.js
define(['jquery', 'underscore', 'marionette', 'app'], function($, _, Mn, app) {
return Mn.Object.extend({
index() {
console.log(app); // undefined
console.log('index method invoked');
/*
i want to do like this
app.showView(new SomeView());
or this.triggerMethod('render', 'someView') and then listen for this event from app.js like this.router.controller.on('render', handler) and dynamic require somehow...
or what is best practice ? iam confused
*/
},
profile() {
console.log('profile method invoked');
},
notFound() {
console.log('notFound method invoked');
}
});
});
Warum nicht geben laden Die Instanz zu Ihrem 'controllers/main.js' Konstruktor? – gpgekko
Entschuldigung? Kannst du ein Beispiel geben ? –
So etwas wie 'const controller = new MainController (instance());' – gpgekko