2

我已经在其中创建了一个新模块,Odoo v8并且我想从static/src/js.

我尝试了以下方法:

1.

function openerp_pos_models(instance){

var myModel = new instance.web.Model('my.model'); 
//code to call python method
});

但是在加载它显示的页面时instance is not defined

2.

var Users = new openerp.web.Model('res.users');

但这表明Uncaught TypeError: Cannot read property 'Model' of undefined

4

3 回答 3

2

您需要将函数调用添加到 widgets.js 文件中。

module.VisitorWidget = module.PosBaseWidget.extend({
    template: 'VisitorWidget',
    init: function(parent, options){
            options = options || {};
            this._super(parent, options);
            this.label   = options.label;
    },
    renderElement: function(){
        var self = this;
        var pos = self.pos;
        var shop = this.pos.get('shop');
        var counter = self.pos.get('visitorcounter');
        var visitors = self.pos.get('visitors',[])
        this.label = counter.toString();
        this._super();
        this.$el.click(function(){
            self.pos.set('visitorcounter', counter + 1);
            var date = new Date();
            obj = { 'visitdate' : date,
                    'count' : 1 ,
                    'shop_id' : shop.id,
            }
            self.pos.get('visitors',[]).push(obj);
            self.renderElement();
        });
    },
    sync_visitors:function(){
        var visitors = self.pos.get('visitors',[]);
        (new instance.web.Model('shop.visitor.history')).get_func('sync_visitors')(visitors)
            .fail(function(unused, event){
                event.preventDefault();
                return;
            })
            .done(function(){
                self.pos.set('visitors',[])
            });
    },
});

这是我的python函数

def sync_visitors(self, cr, uid, visitor, context=None):如果不是访客:返回 False

    for visitor in visitors:
        shop = visitor.get('shop_id',False)
        visitdate = visitor['visitdate']
        count = visitor['count']
        vals = {
                'visitdate' : visitdate,
                'count' : count,
                'shop_id' : shop,
        }
        self.create(cr, uid, vals, context=context)

    return True    

同样,您需要在 models.js 中创建模型。

于 2015-03-23T14:11:01.690 回答
2

问题是:它不是“网络”而是“网站”所以现在你有:

var Users = new openerp.website.Model('res.users');
于 2015-04-29T13:23:43.763 回答
2

这是我的控制器路由功能(在初始化文件中调用):

from odoo import http
from odoo.http import request

class YourClassName(http.Controller):

    @http.route('/your/route/url_here', type='json', auth="user", website=True)
    def your_function_name(self, **kw):
        send_data = kw.get('data')

        // do something here.....

        //return data whatever you want to return....
        return data

这是我的js文件:

odoo.define('igcc.export_view', function(require){
     "use strict"
    var core = require('web.core');
    var ajax = require('web.ajax');

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

    $('.button_name_here').click(function(){
        ajax.jsonRpc("/your/route/url_here", 'call', {'data':you_eant_to_send}).then(function (data) {
            //on successfully return data
        });
    }
});
于 2018-01-12T11:41:19.360 回答