backbone.js - Marionette router not triggering controller method on route -
i'm trying upgrade application marionette 1.* > 2.4.2. biggest part of change shifting away marionette modules using browserify/watchify commonjs style modules.
i've hit roadblock while trying router/controller , running. no error thrown, reason routes arent triggering controller methods.
doc_router.js:
var $ = require('jquery') , _ = require("underscore") , backbone = require("backbone") , marionette = require("backbone.marionette") , app = require("../../app.js") var router = marionette.approuter.extend({ approutes: { "docs": "listdocs" } }); // map controllers var api = { listdocs: function () { console.log("listdocs") // doesnt log console app.docapp.list.controller.listdocs(); } }; // update url when controller event // requires triggered, call // appropriate controller via api. app.on("docs:list", function () { app.navigate("docs"); api.listdocs(); }); // initialize router when app starts app.on("before:start", function () { console.log("history started? ", backbone.history.started); console.log("initializing router...") new router({ controller: api }); }); module.exports = router;
app.js:
var $ = require('jquery') , _ = require("underscore") , backbone = require("backbone") , marionette = require("backbone.marionette") var app = new marionette.application(); app.addregions({ mainregion : "#main-region", modalregion : "#modal-region" }); app.on("start", function () { if (backbone.history) { console.log("application starting..."); var fetchingcurrentuser = app.request("entities:current-user"); $.when(fetchingcurrentuser).done(function (current_user) { app.currentuser = current_user; console.log("starting history..."); backbone.history.start(); console.log("history started? ", backbone.history.started); }); } }); module.exports = app
main.js:
var _ = require('underscore'); var app = require('./app.js'); app.entities = _.extend({}, require("./entities/doc.js"), require("./entities/user.js") ); // docsapp app.docsapp = { "router": require("./apps/doc/doc_router.js"), "reader": { "controller": require("./apps/doc/reader/doc-reader_controller.js"), "views": require("./apps/doc/reader/doc-reader_views.js") }, "list": { "controller": require("./apps/doc/list/doc-list_controller.js"), "views": require("./apps/doc/list/doc-list_views.js") } }; app.start();
judging console logs doesn't seem i'm starting backbone.history before creating router. pulling hair out on one... appreciated.
edit
using backbone.router appears work (below). why isn't marionette.approuter working?
var router = backbone.router.extend({ routes: { "": "listdocs", "docs": "listdocs", "docs/:doc_id" : "readdoc", "docs/:doc_id/edit" : "editdoc" }, listdocs: function () { console.log("listdocs") // logs console // app.docapp.list.controller.listdocs(); } });
marionette extends backbone's built-in router, if want router.navigate
trigger route function, you'll have pass option trigger this:
router.navigate('docs', {trigger: true});
Comments
Post a Comment