Skip to content
Advertisement

Odoo: how in *.js file get record from db, then change it and re-write?

I added new settings for user by inheriting “res.users” model:

calendar_scale_type = fields.Selection([
        ('day', 'Day'),
        ('week', 'Week'),
        ('month', 'Month'),
        ('year', 'Year')],
        'Type of calendar scaling', default='week')

in calendar view i want to read this field and set scale_type when opening this form enter image description here here in calendar_model.js file i want to read this setting from current user enter image description here

and also rewrite current user setting if he chooses diferent type of scale here enter image description here

how can i do this? i tried to use rpc, but i do something wrong and it didn`t work.

Advertisement

Answer

You can override the session_info and add calendar_scale_type to use it later in the calendar model (You will need to override the setScale function).

Example:

  1. Add calendar_scale_type to the session info:

    class Http(models.AbstractModel):
        _inherit = 'ir.http'
    
        def session_info(self):
            session_info = super(Http, self).session_info()
            session_info['calendar_scale_type'] = self.env.user.calendar_scale_type
            return session_info
    
  2. Override setScale function:

    /* @odoo-module */
    
    import CalendarModel from '@calendar/js/calendar_model';
    import { session } from "@web/session";
    
    CalendarModel.include({
    
        setScale: function (scale) {
            if (!_.contains(this.scales, scale)) {
                scale = session.calendar_scale_type;
            }
            this._super(scale);
        },
    
    });
    
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement