我正在尝试从Marionette 1升级应用程序。*> 2.4.2。这一变化的最大部分是从Marionette模块转向使用Browserify / Watchify和CommonJS样式模块。
我试图让路由器/控制器启动并运行时遇到了障碍。不会抛出任何错误,但由于某种原因,我的路由不会触发我的控制器方法。
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 to controllers
var API = {
listDocs: function () {
console.log("listDocs") // Doesnt log to console
App.DocApp.List.Controller.listDocs();
}
};
// Update the URL when a controller event
// that requires it is triggered, then 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();
根据我的控制台日志判断,在创建路由器之前,我似乎并没有启动Backbone.history。真的把我的头发拉出这个...任何帮助表示赞赏。
修改
使用Backbone.Router似乎可以工作(下面)。为什么Marionette.AppRouter不工作?
var Router = Backbone.Router.extend({
routes: {
"": "listDocs",
"docs": "listDocs",
"docs/:doc_id" : "readDoc",
"docs/:doc_id/edit" : "editDoc"
},
listDocs: function () {
console.log("listDocs") // Logs to console
// App.DocApp.List.Controller.listDocs();
}
});
答案 0 :(得分:1)
Marionette扩展了Backbone的内置路由器,因此如果您希望router.navigate
触发路由功能,you'll have to pass the option trigger like this:
router.navigate('docs', {trigger: true});
答案 1 :(得分:1)
结束切换到backbone.base-router,这允许我指定一个控制器,因为Marionette AppRouter显然存在Browserify的问题。