如何将我的路由器绑定到我的方法-Backbonejs
How do I bind the my Router to my method - Backbonejs
我正在尝试将一个方法绑定到我的主干路由器。这是我的课:
define(function(require) {
'use strict';
var Backbone = require('backbone');
var Header = require('views/header.view');
var MainBody = require('views/main.body.view');
var Router = Backbone.Router.extend({
currentView: null,
routes: {
"about": "about"
},
initialize: function() {
Backbone.on('location', this.test);
debugger;
_.bindAll(this, 'test');
},
about: function() {
var header = new Header();
$('#header').html(header.render().el);
},
test: function(data) {
this.currentView.close();
}
});
return Router;
});
在初始化块中,我试图将this
绑定到test
。当我调用test
函数时,我的this
仍然是Backbone
,而不是当前类。我做错了什么?我该如何解决这个问题?
您在附加处理程序后绑定它。
initialize: function() {
// `test` is bound with the wrong THIS
Backbone.on('location', this.test);
debugger;
// Here you actually do the binding
_.bindAll(this, 'test');
},
只需交换订单,看看这是否解决了您的问题。
我认为在上调用Backbone.on时需要第三个参数
要在调用回调时为此提供上下文值,请传递可选的第三个参数:
model.on('change', this.render, this)
像这个Backbone.on('location', this.test, this);
相关文章:
- 为什么可以't我从hamsters.js内部运行我的方法
- 随机数生成器,what'我的方法/统计数据有问题吗?[JS]
- 如何将我的路由器绑定到我的方法-Backbonejs
- 为什么我的C#方法没有从AJAX调用
- 带有方法的 Javascript 对象返回“没有方法'我的方法名称'”错误
- 为什么我的方法看不到这个属性
- 为什么当我将函数添加到Snap.load(..正在加载SVG..我的方法..)中时,我不能调用Snap.Road()内部
- jQuery/AAJAX,检索我的方法返回的字符串
- 卡住了.adddeventlistener不能在属性内处理我的方法.Javascript
- 如何测试我的方法是否在angular2中抛出一个可观察错误
- 为什么我的方法没有返回对象
- 使用if语句和innerHTML来替换内容,我的方法不工作.完全
- 函数没有我的方法可用
- 我如何链接我的方法调用
- 我的方法对xss是完全安全的吗?
- 创建一个对外部世界只读的属性,但我的方法仍然可以设置
- 如何判断我的方法接受yuidoc中的{id: String, label: String}数组
- Ajax 的 Laravel 5 返回视图 - 我的方法很笨拙还是很好
- 为什么我的 ajax post.request 调用我的方法,但没有做任何事情
- 使用裁剪器以减小的尺寸缩放和移动图形 - 是我的方法声音