如何获取模型内部的组件引用保存回调函数
How to get component reference inside of the model save callback functions?
我有一个带有模型的登录组件,该组件会转到服务器,如果登录不正确,则会收到错误。
以下是我所说的想法:
var LoginModel = can.Model.extend({
create : "POST /account/login"
},{});
can.Component.extend({
tag: "pod-login",
template: can.view("/static/js/views/login_form.stache"),
viewModel:{
login: new LoginModel(),
processLogin: function(login) {
// I need to access the component here
},
processLoginError: function(response) {
// I need to access the component here
}
},
events: {
"#login_button click": function() {
var form = this.element.find( 'form' );
var values = can.deparam(form.serialize());
this.viewModel.login.attr(values).save(
this.viewModel.processLogin,
this.viewModel.processLoginError
);
}
}
});
这里的问题是,当我试图在模型登录处理程序中使用"this"时,我得到的对象不是当前组件实例。例如,在项目LogiError上,我得到了xhr引用。
如何访问processLogin和processLoginError中的组件?
我的解决方法是在login_button点击事件中使用$('some_html_element_on_My_template').data('component',this),并在回调函数中访问它,但我认为这可以更好地处理。
有什么见解吗?
您需要将上下文绑定到回调:
this.viewModel.login.attr(values).save(
this.viewModel.processLogin.bind(this),
this.viewModel.processLoginError.bind(this)
);
别忘了在IE8中包含es5-shim
。
相关文章:
- 对自定义组件中的本地访问引用进行反应
- 了解如何引用 react 组件中的属性
- 如何引用 OpenUI5 组件
- 如何处理 React 0.12+ 中的组件引用变量和 props
- 组件在 Angular 2 中如何相互引用
- React:新安装的子组件无法访问父组件引用
- 如何获取模型内部的组件引用保存回调函数
- 未呈现React引用子组件
- 如何为javascript组件引用HTML层
- 组件对话框无法在IE中运行(无法获取属性'clear'未定义或空引用)
- Angular 2的组件变量作用域和引用
- 从组件内部访问其他组件引用
- 组件引用错误
- 在React中引用组件外部的变量
- 如何引用文档.在React组件中工作
- 我如何引用连接(容器)组件's道具内mapStateToProps
- 如何从angular指令或组件中引用特定类型的子控制器
- 如何在初始设置后引用(子)Sencha Touch中的组件
- 如何获取父组件引用
- 将二进制组件引用到js-cypes