콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
3 답글
6762 화면

I'm very bad at JS. I've looked at the mail modules systray icons javascript and template files. I've read the javascript cheetsheet and referance documentation but I cannot get it to work.

I want to gave a systray icon that shows a number same as the mail systray icons and when you click on it, a div with the message apears. The number and message need to be gotten from a models method. This means that I've got a problem with asynchronous promise function. The custom module returns json dump of a dict

JS Code:

odoo.define('odoo13_demo_timer.DemoDays', function (require) {
    'use strict';

    var core = require('web.core');
    var session = require('web.session');
    var SystrayMenu = require('web.SystrayMenu');
    var Widget = require('web.Widget');
    var Qweb = core.qweb;

    var DemoDays = Widget.extend({
        name: 'demo_days_menu',
        template: 'odoo13_demo_timer.DemoDays',
        events: {
            'show.bs.dropdown': '_onDemoDaysShow',
            'hide.bs.dropdown': '_onDemoDaysHide',
        },
        start: function() {
            this._getDemoDaysData();
            return this._super();
        },
        _getDemoDaysData: function() {
            var self = this;

            return self._rpc({
                model: 'demo.installation',
                method: 'get_systray_dict',
                args: [],
                kwargs: {context: session.user_context},
            }).then(function (data) {
                self.demo_days_data = data;
                self.$('.o_demo_days_counter').addClass(data.message_class);
                self.$('.o_demo_days_counter').text(data.expires);
                self.$('.demo_days_message').append(data.message);
            });
        },
        _onDemoDaysShow: function () {
            document.body.classList.add('modal-open');
        },
        _onDemoDaysHide: function () {
            document.body.classList.remove('modal-open');
        },
    });
    DemoDays.prototype.sequence = 100;
    SystrayMenu.Items.push(DemoDays);

    return DemoDays;
});

Template:

<?xml version="1.0" encoding="UTF-8"?>
<templates>
    <t t-name="odoo13_demo_timer.DemoDays">
        <li class="o_timer_systray_item">
            <a class="dropdown-toggle o-no-caret" data-toggle="dropdown" data-display="static" aria-expanded="false" title="Demo Days" href="#" role="button">
                <i class="fa fa-history" role="img" aria-label="Demo Days"/> <span class="o_demo_days_counter badge badge-pill"/>
            </a>
            <div class="o_demo_days_systray_dropdown dropdown-menu dropdown-menu-right">
                <span class="demo_days_message"/>
            </div>
        </li>
    </t>
</templates>

If I hard code the number and message in the start function it works. Getting the data from the model doesn't work. I wasted hours with trying different things but I just cannot get it  to work.

start: function() {
    this.$('.o_demo_days_counter').addClass('green');
    this.$('.o_demo_days_counter').text('15');
    this.$('.demo_days_message').append('some text for message');
},


Can someone help?  


EDIT:

The data is now accessible, when I updated the code with the answer for promise. But it still does not show the data!? Really need help with this.


아바타
취소
작성자

Finally I've got it to work.

Used @Ravi Gadhia answer for help with the promise. Moved the promise to willStart and only set the DOM elements in the start.

베스트 답변
start: function() {
var self = this;
return Promise.all([this._super.apply(this, arguments), this._getDemoDaysData()]).then(function () {
self.$('.o_demo_days_counter').addClass(self.demo_days_data.message_class);
self.$('.o_demo_days_counter').text(self.demo_days_data.expires);
self.$('.demo_days_message').append(self.demo_days_data.message);
});
});

},
_getDemoDaysData: function() { var self = this;
return this._rpc({
model: 'demo.installation',
method: 'get_systray_dict',
args: [],
kwargs: {context: session.user_context},
}).then(function (data) {
self.demo_days_data = data;
})
},

try this it will resolve if there is promise issue note: please corrent sytext error if any
아바타
취소
작성자

Thanks, I have the data but it still does not show the counter or message. You have one to many }); in the return Promise.

Can you please post the get_systray_dict function?

작성자

@api.model

def get_systray_dict(self):

demos_obj = dict()

demo_install = self.with_user(SUPERUSER_ID).search([])

if not demo_install:

return json.dumps(demos_obj)

demo_install = demo_install[0]

now = datetime.now().date()

delta = now - demo_install.database_creation

demos_obj['name'] = demo_install.name

demos_obj['days_ago'] = delta.days

demos_obj['expires'] = EXPIRATION_DAYS - demos_obj['days_ago']

demos_obj['deletes'] = DELETION_DAYS - demos_obj['days_ago']

demos_obj['message'] = u'MESSAGE'

if demos_obj['expires'] <= 3:

demos_obj['message_class'] = 'red'

elif demos_obj['expires'] <= 9:

demos_obj['message_class'] = 'yellow'

elif demos_obj['expires'] > 9:

demos_obj['message_class'] = 'green'

else:

demos_obj['message_class'] = 'green'

return json.dumps(demos_obj)

관련 게시물 답글 화면 활동
1
5월 20
4360
2
2월 20
5982
0
12월 19
4237
1
6월 24
3601
1
6월 22
2972