路由器不使用require js和backbone

时间:2014-06-10 15:00:24

标签: backbone.js require router

Backbone版本1.1.2 需要2.0.2版本

//我的主文件

  require.config({
  paths: {
    jquery: 'assets/js/libs/jquery/jquery-1.9.1.min',
    underscore: 'assets/js/libs/underscore/underscore-min',
    backbone: 'assets/js/libs/backbone/backbone-min',
    bootstrap: 'assets/js/libs/bootstrap/bootstrap.min',
    text      : 'assets/js/libs/require/text',
    mustache: 'assets/js/libs/mustache/mustache'
  },
  shim: {
        'backbone': {
            deps: ['jquery','underscore'],
            exports: 'Backbone'
        },
        'bootstrap': {
            deps: ['jquery']
        }
  }

});

require([

  // Load our app module and pass it to our definition function
  'assets/js/app',
], function(App){

  App.initialize();

});

//我的应用文件

define([
  'jquery',
  'underscore',
  'backbone',
  'assets/js/router'
], function($, _, Backbone, Router){
  var initialize = function(){

    Router.initialize();
 }

  return {
    initialize: initialize
  };
});

//我的路由器文件

define([
  'jquery',
  'underscore',
  'backbone',
  'assets/js/views/trabajadores/list'
], function($, _, Backbone, TrabajadoresListView){

  var AppRouter = Backbone.Router.extend({

    routes:{

      '':'mostrarTrabajadores',
      'trabajadores': 'mostrarTrabajadores'
    },

    mostrarTrabajadores: function(){

      var trabajadoresListView = new TrabajadoresListView();
      trabajadoresListView.render();
    }

  });

  var initialize = function(){

    var app_router = new AppRouter; 
    Backbone.history.start();

  };

  return {
    initialize: initialize
  };

});

我正在检查代码,当下面的代码行执行“var app_router = new AppRouter;”时,控制台说:Uncaught TypeError:undefined不是函数。

路由器它不适合我。为什么?

1 个答案:

答案 0 :(得分:0)

您可以在initialize方法中声明路由操作。只需使用以下内容:

var initialize = function(){
  var app_router = new AppRouter;

  app_router.on('route:index', function() {
    console.log('index route');
  });

  return {
    initialize: initialize
  };

});

请确保在路由器的“on()”方法中遵循“route:Action”格式。