我正在尝试调用router.on('route', callback)
函数,但路由没有提供回调...
我的路由器/ app:
var Backbone = require('backbone')
Backbone.$ = $
var _ = require('underscore')
var App = Backbone.Router.extend({
// nothing here yet
}, {
instance: null,
getInstance: function () {
App.instance = App.instance || new App
return App.instance
}
})
module.exports = App
使用路由器的一些模块:
var IndexView = require('./views/IndexView')
var App = require('../../AppModule/js/main')
var _ = require('underscore')
var Backbone = require('backbone')
Backbone.$ = $
function IndexModule(opts) {
this.app = App.getInstance()
this.indexView = new IndexView({
view: opts.view
})
this.init = function() {
this.app.route('', _.bind(this.indexRoute, this))
this.app.route('testroute#:id', function(id) {
console.log(id)
})
this.app.on('route', _.bind(this.routeChanged, this))
}
this.indexRoute = function() {
this.indexView.render()
}
this.routeChanged = function() {
console.log(arguments)
}
}
module.exports = IndexModule
某处$(document).ready()
:
var indexModule = new IndexModule({
view: "someview.html"
})
indexModule.init()
Backbone.history.start({root: '/'});
如果我转到testroute/2
,我会得到以下结果:
["", ["2"]]
不应该将路径(testroute)作为字符串出现在参数中吗?
谢谢..
答案 0 :(得分:0)
刚刚意识到您已将路径设置为testroute /:id。由于id是这里唯一的变量,因此只将它作为参数传递给处理程序。您应该尝试像:routeName/:id
这样的方式,路由名称和ID都将作为参数传递。