Tuesday 15 May 2012

javascript - Passing app object in requirejs + marionette application -



javascript - Passing app object in requirejs + marionette application -

working marionette/backbone , having problem passing app object in order trigger custom event.

specifically, requirejs throwing following: "error: module name "app" has not been loaded yet context: _".

edit: offending code "var app = require('app');" within appcontroller.js.

from i've read, message refers circular reference or script not beingness loaded yet. how should construction code avoid this?

note 1: @ moment, i'm not using r.js. have grunt build process, haven't had chance set r.js. mentioning in case alleviate issue.

note 2: i'm not using marionette's stock module because wanted larn requirejs.

thanks in advance.

//config.js require.config({ paths: { jquery: '../jquery', bootstrap: '../bootstrap', underscore: '../lodash', backbone: '../backbone', 'backbone.babysitter': '../backbone.babysitter', 'backbone.wreqr': '../backbone.wreqr', marionette: '../backbone.marionette', text: '../text' }, enforcedefine: true, shim: { 'bootstrap': { deps: ['jquery'], exports: '$' }, } }); define( ['app', 'jquery', 'underscore', 'backbone', 'marionette', 'bootstrap'], function(app, $, _, backbone, marionette) { app.start(); } ); //app.js define(function(require) { var marionette = require('marionette'), // , approuter, appcontroller var app = new marionette.application(); app.addinitializer(function() { var router = new approuter({ controller: appcontroller }); }); app.on('start', function() { backbone.history.start(); }); app.vent.on('custom:event', function(a) { console.log('caught custom:event, received: ' + a); }); homecoming app; }); // approuter.js define(function(require) { var marionette = require('marionette'); homecoming marionette.approuter.extend({ approuter: { '': 'main' } }); }); //appcontroller.js define(function(require) { homecoming { main: function() { var app = require('app'); app.vent.trigger('custom:event', ['test']); } } });

update:

of course of study after posting question, find possible solution. if adjust appcontroller.js following, works without issue. still, solution doesn't sense right. have duplicate every route?

//appcontroller.js (version 2) define(function(require) { homecoming { main: function() { require(['app'], function(app) { app.vent.trigger('custom:event', ['test']); }); } } });

update 2

if curious, how got working backbone.wreqr (thank @arisalexis). showing files changed code above.

//app.js define(function(require) { var marionette = require('marionette'), wreqr = require('backbone.wreqr'), // , approuter, appcontroller var app = new marionette.application(); app.addinitializer(function() { var router = new approuter({ controller: appcontroller }); }); app.on('start', function() { backbone.history.start(); }); // hook global channel , hear events! var channel = wreqr.radio.channel('global'); channel.vent.on('custom:event', function(a) { console.log('caught custom:event, received: ' + a); }); homecoming app; }); //appcontroller.js define(function(require) { var wreqr = require('backbone.wreqr'); var channel = wreqr.radio.channel('global'); homecoming { main: function() { channel.vent.trigger('custom:event', 'test'); } } });

note, in general, router's controller should not have logic in it. code presented in question proof of concept.

i had same problem , not solve it, luckily because deprecated in version 3.

from docs:

to access application channel other objects within app encouraged handle of systems through wreqr api instead of application instance itself.

var globalch = backbone.wreqr.radio.channel('global'); globalch.vent;

use or take @ https://github.com/marionettejs/backbone.radio

javascript backbone.js requirejs marionette

No comments:

Post a Comment