使用木偶对象在木偶中进行路由 - 无法使其正常工作

时间:2015-10-25 16:08:31

标签: backbone.js routing marionette

我第一次尝试木偶,我对它的路由非常困惑。

这是我到目前为止所拥有的:一个Backbone模型,一个Backbone集合,一个Marionette Layout视图,一个Marionette Collection View,一对Marionette Item视图。这些视图在Marionette对象中连接在一起,如下所示:

var Orchestrator = Marionette.Object.extend({
  initialize: function(layout) {
    this.layout = layout;
    // adding a collection view
    // showing the collection view in the layout view

    // all is honky-dory
  },

  // And here is a test function I want to call when I go to localhost:8080/#test:

  test: function() {
    console.log('HELLOOO!!!');
  }

});

这就是混乱。我正在尝试设置路由器:

var Router = Marionette.AppRouter.extend({
  initialize: function(controller) {
    // the 'controller' is an instance of my Orchestrator object defined above
    this.controller = controller;
    console.log(this.controller.test); // the method shows up correctly
    this.appRoutes = {
      'test': 'test'
    };
  }
});

然后我初始化路由器:var router = new Router(orchestrator);并启动Marionette应用程序。

路由器中的console.log语句显示1)它正在被初始化,2)'controller'是正确的Marionette对象,以及3)'controller'具有方法test on它

但是,当我导航到localhost:8080/#test(我的应用程序在localhost:8080处提供)时,我看不到任何证据表明test对象的Orchestrator方法有被叫 - 我的'HELLO'消息没有出现在控制台中。控制台中也没有错误。

请问你可以提出一些问题,以及如何解决这个问题?我一直在搜索文档,但到目前为止还没有找到任何答案。

1 个答案:

答案 0 :(得分:0)

D'哦!忘了给Backbone.history.start();打电话。愚蠢的我!