Backbone.js 主干在路线上,没有在回调中给我路线
我正在尝试调用Backbone.js 主干在路线上,没有在回调中给我路线,backbone.js,Backbone.js,我正在尝试调用路由器。on('route',callback)函数,但该路由未提供回调 我的路由器/应用程序: var Backbone = require('backbone') Backbone.$ = $ var _ = require('underscore') var App = Backbone.Router.extend({ // nothing here yet }, { instance: null, getInstance: function () {
路由器。on('route',callback)
函数,但该路由未提供回调
我的路由器/应用程序:
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
表单$(文档).ready()
:
如果我转到testroute/2
我会得到以下结果:
["", ["2"]]
路由(testroute)不应该作为字符串出现在参数中吗
谢谢..刚刚意识到您已经将路由设置为testroute/:id。由于id是这里唯一的变量,因此只有id作为参数传递给处理程序。您应该尝试类似于
:routeName/:id
的方法。这样,路由名称和id都将作为参数传递。您绑定到的路由是testroute#:id。它应该是testroute/:id。我尝试了两者,两者都不起作用
["", ["2"]]