Reputation: 1843
I have created a new module in Odoo v8
and I want to call a python function defined in the controllers folder of the module from a JavaScript file defined in static/src/js
.
I have tried the following:
1.
function openerp_pos_models(instance){
var myModel = new instance.web.Model('my.model');
//code to call python method
});
But on loading the page it shows instance is not defined
.
2.
var Users = new openerp.web.Model('res.users');
but this shows Uncaught TypeError: Cannot read property 'Model' of undefined
.
Upvotes: 2
Views: 5816
Reputation: 438
Here is my conroller route function(call in init file):
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
Here is my js file:
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
});
}
});
Upvotes: 2
Reputation: 3212
The problem is : it's not 'web' but 'website' So now you have :
var Users = new openerp.website.Model('res.users');
Upvotes: 2
Reputation: 14746
You need to add your function calling into widgets.js file.
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',[])
});
},
});
And this is my python function
def sync_visitors(self, cr, uid, visitors, context=None): if not visitors: return 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
And for same you need to create model into the models.js.
Upvotes: 2