简体   繁体   中英

Router in backbone doesnot work

I'm sure that jquery , underscore and backbone in my main.js are working with require.js . Here my router :

define([
 'jquery',
 'underscore',
 'backbone',
 'views/CartList'
], function($, _, Backbone,CartList){
var AppRouter = Backbone.Router.extend({
    routes: {
        "showCart" : "cartlist"
    }
});
// Initiate the router
var initialize = function(){
    var app_router = new AppRouter;
    app_router.on('cartlist', function(){
        alert(1);
    });
    Backbone.history.start();
};
return {
    initialize: initialize
};
});

Here app.js :

define(["jquery", "backbone", "router"], function($, Backbone, Router) {
  var initialize = function(){
    Router.initialize();
  }
  return {
    initialize: initialize
  };
});

When I browse www.mysite.com/#showCart , It didnot show the alert .

Any help would be appreciated.

Try to add route: prefix

app_router.on('route:cartlist', function () {
  alert(1);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM