带有require.js的木偶/骨干路由和控制器

时间:2013-11-29 16:17:02

标签: backbone.js routing requirejs marionette

有人可以解释/提供一个例子,说明如何让这个路由工作在Marionette工作。

导航到/ #test后我想得到console.log(“stuff”)。 这就是我到目前为止所做的事情,它绝对没有任何意义(尽管......但没有错误):

main.js:

requirejs.config({
  baseUrl: '/',
  paths: {
    'text': '../vendor/javascripts/requirejs-text/text',
    'backbone': '../vendor/javascripts/backbone/backbone',
    'backbone.wreqr': '../vendor/javascripts/backbone.wreqr/backbone.wreqr',
    'backbone.babysitter': '../vendor/javascripts/backbone.babysitter/backbone.babysitter',
    'jquery': '../vendor/javascripts/jquery/jquery',
    'jquery-ui': '../vendor/javascripts/jquery-ui/jquery-ui',
    'json2': '../vendor/javascripts/json2/json2',
    'marionette': '../vendor/javascripts/marionette/backbone.marionette',
    'underscore': '../vendor/javascripts/underscore/underscore',
    'handlebars': '../vendor/javascripts/handlebars/handlebars'
  },
  shim: {
    'underscore': {
      exports: '_'
    },
    'backbone': {
      deps: ['jquery', 'underscore', 'json2'],
      exports: 'Backbone'
    },
    'marionette': {
      deps: ['backbone'],
      exports: 'Marionette'
    },
    'jquery-ui': {
      deps: ['jquery']
    },
    'handlebars': {
      exports: 'Handlebars'
    }
  }
});

define(["app"], function(App) {
  return App.start();
});

app.js:

define(['underscore', 'jquery', 'backbone', 'marionette', 'view', 'router'], function(_, $, Backbone, Marionette, View, Router) {
  var App;
  App = new Backbone.Marionette.Application();
  App.on("initialize:after", function() {
    var router, view;
    view = new View();
    return router = new Router();
  });
  return App;
});

router.js:

define(["marionette", "controller"], function(Marionette, Controller) {
  var AppRouter;
  AppRouter = Backbone.Marionette.AppRouter.extend({
    controller: Controller,
    appRoutes: {
      'test': 'testStuff'
    },
    initialize: function() {
      return console.log('router init');
    }
  });
  return AppRouter;
});

controller.js:

define ["marionette"], (Marionette) ->

    Controller = Marionette.Controller.extend
        initialize: ->
            console.log 'controller initialized'

        testStuff: ->
            alert 'stuff'


    # create an instance
    Controller = new Controller()

1 个答案:

答案 0 :(得分:3)

所以这件事得到了解决:

需要添加

if (Backbone.history) { 
  Backbone.history.start();
}
初始化路由器后