简体   繁体   English

Odoo10-如何使用JavaScript

[英]Odoo10 - How to do javascript

I must be doing something completely wrong: 我一定在做完全错误的事情:

odoo.define('my_module.popups', function (require) {
    'use strict';
    var ajax = require('web.ajax');
    var core = require('web.core');
    var _t = core._t;
    var qweb = core.qweb;
    ajax.loadXML('/my_module/static/xml/templates.xml', qweb);

    var data = {modal_title: 'This is a popup!',modal_body: 'testtest'};
    var p = qweb.render("my_module.popup1_template", data);
    p.prependTo('body');
});

I'm not sure I understand this. 我不确定我是否理解这一点。 The code inside define is never executed. define中的代码永远不会执行。 I read many docs and examples, on how to create a Widget etc. But the documentation never explains how do you use/call this stuff that you put inside the 'define'. 我阅读了许多有关如何创建Widget等的文档和示例。但是文档从未解释过如何使用/调用放在“ define”中的内容。

I could also just manually create a popup and prepend it to the body element, but I want to do this the odoo way. 我也可以手动创建一个弹出窗口,并将其添加到body元素之前,但是我想使用odoo方法。

I hear you, I think the secrets of Odoo's js framework are the secret weapon a lot of people like to keep to themselves. 我听到了您的声音,我认为Odoo的js框架的秘密是许多人喜欢保留的秘密武器。 I am sure it is all completely obvious if you spent the last 4 years working with backbone, requirejs and underscore. 我敢肯定,如果您在过去的4年中使用了骨干,requirejs和下划线,这一切都是显而易见的。 Sadly thats not me. 可惜那不是我。

If you take a look at the notification module in /addons/web/static/src/js/widgets/notification.js you should be able to see what they are doing. 如果您查看/addons/web/static/src/js/widgets/notification.js中的通知模块,您应该能够看到它们在做什么。 Some things that might help you are put some logging in to see if your scripts are being loaded and when. 有些可以帮助您的事情已登录,以查看是否正在加载脚本以及何时加载脚本。 For what you are trying to do you will need to provide some events mapping. 对于您要尝试执行的操作,您将需要提供一些事件映射。 There is an example in the file I mentioned. 我提到的文件中有一个示例。 In your jsmodule you will create an object which has an events attribute looking something like this. 在您的jsmodule中,您将创建一个对象,该对象的事件属性如下所示。

events: {
    'click .o_close': function(e) {
        e.preventDefault();
        this.destroy(true);
    },
    'hover .my_widget_class': function(e){
        // your code here
    },
},

Do not take the above code literally. 不要从字面上理解上面的代码。 You need an event that triggers you widget to be appended to dom at some point. 您需要一个事件来触发您的小部件在某个时候附加到dom。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM