Odoo10-如何使用JavaScript [英] Odoo10 - How to do javascript

查看:235
本文介绍了Odoo10-如何使用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');
});

我不确定我是否理解这一点. define中的代码永远不会执行.我阅读了许多有关如何创建Widget等的文档和示例.但是文档从未解释过如何使用/调用放在定义"中的这些东西.

I'm not sure I understand this. The code inside define is never executed. 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'.

我还可以手动创建一个弹出窗口,并将其添加到body元素的前面,但是我想通过oodoo方式做到这一点.

I could also just manually create a popup and prepend it to the body element, but I want to do this the odoo way.

推荐答案

我听到了,我认为Odoo的js框架的秘密是许多人喜欢保留的秘密武器.我敢肯定,如果您在过去的4年中使用了骨干,requirejs和下划线,这一切都是显而易见的.可悲的是,那不是我.

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. I am sure it is all completely obvious if you spent the last 4 years working with backbone, requirejs and underscore. Sadly thats not me.

如果您查看/addons/web/static/src/js/widgets/notification.js中的通知模块,您应该能够看到它们在做什么.可能会帮助您的某些事情已登录,以查看是否正在加载脚本以及何时加载脚本.对于您要尝试执行的操作,您将需要提供一些事件映射.我提到的文件中有一个示例.在您的jsmodule中,您将创建一个对象,该对象具有一个类似于以下内容的events属性.

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. 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.

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

请勿直接使用上面的代码.您需要一个事件来触发您的小部件在某个时候附加到dom.

Do not take the above code literally. You need an event that triggers you widget to be appended to dom at some point.

这篇关于Odoo10-如何使用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆