I am also looking for a good way to handle such situations using a puppet and require.js
I worked on a solution, but I donβt know if this is the best way, here are my thoughts:
- the application has actions related to events, not knowing about the views
- inside the view, we use triggers to attach actions to events
- the combination of actions between the view and the application is done inside the view
This is a possible solution:
app.js
define( [ 'underscore', 'jquery', 'backbone', 'marionette' , 'view'], function( _, $, Backbone, Marionette, View ) { var app = new Marionette.Application(); app.addRegions({ content: '#content'}) app.on( "initialize:after", function(){ console.log( 'after init', app) var view = new View(); app.content.show( view ); });
view.js
define( [ 'underscore', 'jquery', 'backbone', 'marionette' ], function( _, $, Backbone, Marionette ) { var View = Marionette.ItemView.extend({ template: '#view', triggers: { 'click': 'clicked' }, initialize: function(){
It is important to remember that require is asynchronous, so when we use it, it will not be executed immediately:
require( ['some-module'], function( someModule ){
we can prepare objects that point to an external context, for example:
var thisName = this; require( ['some-module'], function( someModule ){