如何在新创建的模块中从Odoo v8中的Javascript文件调用python函数?

时间:2015-03-23 12:26:04

标签: javascript python odoo openerp-8 odoo-8

我在Odoo v8中创建了一个新模块,我想从static/src/js中定义的JavaScript文件中调用模块的controllers文件夹中定义的python函数。

我尝试了以下内容:

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

3 个答案:

答案 0 :(得分: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中创建模型。

答案 1 :(得分:2)

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

if (sessionUserLogin == 'True') {
       $('#myTableId').dataTable({ "bStateSave": false, "bDestroy": true});            
       $('#myTableId').dataTable({ "bStateSave": true });
  }
   else {
         $("#myTableId").dataTable({ "bStateSave": true });
  }

答案 2 :(得分:2)

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

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
        });
    }
});