直奔主题: instance.web.client_actions.add("change_password", "instance.web.ChangePassword");
有个动作叫做change_password,把它的请求最终(在客户端)给instance.web.ChangePassword"给处理了。
1. 看看后台py代码:
\addons\base\res\res_users.py (481~486)
def preference_change_password(self, cr, uid, ids, context=None):
return {
'type': 'ir.actions.client',
'tag': 'change_password',
'target': 'new',
}
类型是 ir.actions.client
2. qweb
xml: \addons\web\static\src\xml\base.xml (333~363)
js: \addons\web\static\src\js\chrome.js (819~852)
instance.web.ChangePassword = instance.web.Widget.extend({
template: "ChangePassword",
start: function() {
var self = this;
this.getParent().dialog_title = _t("Change Password");
var $button = self.$el.find('.oe_form_button');
$button.appendTo(this.getParent().$buttons);
$button.eq(2).click(function(){
self.getParent().close();
})
$button.eq(0).click(function(){
self.rpc("/web/session/change_password",{
'fields': $("form[name=change_password_form]").serializeArray()
}).done(function(result) {
if (result.error) {
self.display_error(result);
return;
} else {
instance.webclient.on_logout();
}
});
})
},
display_error: function (error) {
return instance.web.dialog($('<div>'), {
modal: true,
title: error.title,
buttons: [
{text: _t("Ok"), click: function() { $(this).dialog("close"); }}
]
}).html(error.error);
},
})
instance.web.client_actions.add("change_password", "instance.web.ChangePassword");
<t t-name="ChangePassword">
<form name="change_password_form" method="POST">
<div class="oe_form">
<table align="center">
<tr>
<td class="oe_form_group_cell oe_form_group_cell_label"><label for="old_pwd" class="oe_form_label">Old Password:</label></td>
<td class="oe_form_group_cell"><input type="password" name="old_pwd"
minlength="1" autofocus="autofocus"/></td>
</tr>
<tr>
<td class="oe_form_group_cell oe_form_group_cell_label"><label for="new_password" class="oe_form_label">New Password:</label></td>
<td class="oe_form_group_cell"><input type="password" name="new_password"
minlength="1"/></td>
</tr>
<tr>
<td class="oe_form_group_cell oe_form_group_cell_label"><label for="confirm_pwd" class="oe_form_label">Confirm New Password:</label></td>
<td class="oe_form_group_cell"><input type="password" name="confirm_pwd"
minlength="1"/></td>
</tr>
<tr>
<td colspan="2" align="right">
<button class='oe_button oe_form_button'>Change Password</button>
<span class="oe_fade oe_form_button"> or </span>
<button type="button" class="oe_button oe_form_button_cancel oe_form_button oe_link" href="javascript:void(0)"><span>Cancel</span></button>
</td>
</tr>
</table>
</div>
</form>
</t>
4. widget代码分析:
5. 细节
1). serializeArray
The serializeArray() method creates an array of objects (name and value) by serializing form values
将表单的input元素转为为{'name':input标签的name, 'value':input标签的value)
2).$button.appendTo(this.getParent().$buttons); 此处,讲按钮添加到form的$buttons里面,其原始html为:
"<span class="oe_dialog_custom_buttons"></span>" 此处用法不明。
3). rpc 调用路线:
widget -> instance.web.Controller(.rpc) -> instance.web.Session(.rpc) -> instance.web.JsonRPC( rpc) -> $.ajax(ajax)
ajax 结构
作者:深圳-Tommy
邮箱:lion_awake@163.com
有问题可以和我讨论,转载请注明出处,谢谢!