2013-01-19 157 views
1

我有一个使用require js的骨干路由器。一切似乎都很好,但它不工作。我从我的app.js调用router骨干路由器不能使用requirejs

路由器JS:

define([ 
    'jquery', 
    'underscore', 
    'backbone', 
    'view/questions/index' 
], function($, _, Backbone, IndexView){ 
var AppRouter = Backbone.Router.extend({ 
    routes: { 
     '/': 'index' 
    } 
}); 

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

    // Index Route 
    app_router.on('index', function(){ 
     var indexView = new IndexView(); 
     console.log('test'); 
     indexView.initialize(); 
    }); 

    // Default Route 
    app_router.on('defaultAction', function(actions){ 
     console.log('No Route', actions); 
    }); 

    Backbone.history.start(); 
}; 

return { 
    initialize: initialize 
}; 
}); 

应用JS:

define([ 
    'jquery', 
    'underscore', 
    'backbone', 
    'router' 
], function($, _, Backbone, Router){ 
var initialize = function(){ 
    Router.initialize(); 
}; 

return { 
    initialize: initialize 
}; 
}); 
+0

难道不requirejs这项工作?我一直亲自在路由器上使用事件哈希,但是看看源代码,似乎如果你想绑定事件,它们需要以“route:”作为前缀。 'on(“route:index”)',而不仅仅是“索引”。我不是一个骨干专家,也许其他版本不是这样的。 – numbers1311407

+0

其实我已经得到了这个工作。问题出在'/'上,必须被称为'(“route:index”)'。队友的欢呼声。 – Subash

回答

5

家伙其实,我得到了这个工作。这是我在代码中所做的更改。

var AppRouter = Backbone.Router.extend({ 
    routes: { 
     '': 'index' 
    } 
}); 

这里:

app_router.on('route:index', function(){ 
    var indexView = new IndexView(); 
    console.log('test'); 
    indexView.initialize(); 
});