简体   繁体   English

Odoo-12:我创建了一个小部件。 我如何使用它?

[英]Odoo-12: I created a widget. How do I use it?

I created a custom module that defines a widget.我创建了一个定义小部件的自定义模块。 here are the .js file as well as the QWeb template:这是 .js 文件以及 QWeb 模板:

bh_sidebar.js bh_sidebar.js

odoo.define('sidebar_widget.bh_sidebar', function (require) {
    'use strict';
    var Widget = require('web.Widget');
    var widgetRegistry = require('web.widget_registry');
    var core = require('web.core');

    var QWeb = core.qweb;
    var _t = core._t;

    var bh_sidebar = Widget.extend({
        init: function () {
            var self = this;
            this._super(parent);
            console.log('Widget initialized!');
        },

        events: {},

        start: function () {
            this.$el.append(QWeb.render('bh_sidebar_template'));
            console.log('Widget started!');
        }

    });

    widgetRegistry.add(
        'bh_sidebar', bh_sidebar
    )
})

bh_sidebar.xml bh_sidebar.xml

<?xml version="1.0"?>
<templates id="template" xml:space="preserve">
    <t t-name="bh_sidebar_template">
        <div style="background-color:red;" class="myClass">
            <a href="#" class="butt1">Button 1</a>
            <a href="#" class="butt2">Button 2</a>
        </div>
    </t>
</templates> 

I've added both to the backend assets using:我已使用以下方法将两者都添加到后端资产中:

<odoo>
    <data>
        <template id="assets_backend" inherit_id="web.assets_backend">
            <xpath expr="script[last()]" position="after">
                <script type="text/javascript" src="/sidebar_widget/static/src/js/bh_sidebar.js"></script>
                <!-- <script type="text/javascript" src="/sidebar_widget/static/src/js/renderer.js"></script> -->
                <link rel="stylesheet" type="text/scss" href="/sidebar_widget/static/src/scss/widget.scss"></link>
            </xpath>
        </template>
    </data>
</odoo>

I've also added the templates to the manifest file.我还将模板添加到清单文件中。

This widget is supposed to display a red rectangle with two links, button 1 and button 2. I want this widget to be independent of any type of view, and just show up at the top of the screen beneath Odoo's navigation bar.这个小部件应该显示一个带有两个链接的红色矩形,按钮 1 和按钮 2。我希望这个小部件独立于任何类型的视图,并且只显示在 Odoo 导航栏下方的屏幕顶部。 How do I that?我怎么办? if I try to run my code as is, nothing happens.如果我尝试按原样运行我的代码,则没有任何反应。 I've been at this for a week.我已经在这一个星期了。 Nothing I found on the internet helped.我在互联网上找到的没有任何帮助。 The documentation is criminally outdated, the ressources scarce.文件已经过时,资源稀缺。

I would appreciate any help.我将不胜感激任何帮助。 Thank you.谢谢你。

Your widget will not be instantiated and inserted into the DOM automatically.您的小部件不会被实例化并自动插入到 DOM 中。 To do this, you can hook into any of the already existing high-level widgets.为此,您可以连接到任何现有的高级小部件。 For example, using the show_application method on the WebClient:例如,在 WebClient 上使用show_application方法:

var WebClient = require('web.WebClient');

WebClient.include({

    show_application: function () {
        var sup = this._super.apply(this, arguments);

        // Instantiate
        var sidebar = new bh_sidebar();

        // Insert in DOM
        sidebar.insertAfter(this.$('.o_main'));

        return sup;
    },

});

我认为你应该通过使用控制器机制来呈现这个视图,这将使这个视图独立于其他视图。

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

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