Hello everyone,
i'm building a snippet that display latest published products (ecommerce products)
ok, let's start
addons/my_theme/views/snippets.xml
<template id="ecommerce_list" name="Ecommerce List" inherit_id="website.snippets">
<xpath expr="//div[@id='snippet_structure']/div[@class='o_panel_body']" position="inside">
<t t-snippet="my_theme.latest_products_list"/>
</xpath>
</template>
addons/my_theme/views/templates.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="latest_products_list" name="Ecommerce List">
<section>
<div classe="row">
<h2> Latest Produtcts </h2>
<t t-foreach="values" t-as="product">
<h2 t-field="product.name"/>
<img t-attf-src="data:image/*;base64,{{product.image_medium}}"/>
</t>
</div>
</section>
</template>
<template id="snippet_js" inherit_id="website.assets_editor" name="Snippet js">
<xpath expr="." position="inside">
<script type="text/javascript" src="/my_theme/static/src/js/snippet.js" />
</xpath>
</template>
</odoo>
addons/my_theme/controllers/controllers.py
# -*- coding: utf-8 -*-
from odoo import http
from odoo.addons.website_sale import models, views
from odoo.http import request
class MyTheme(http.Controller):
@http.route(['/my_theme/ecommerce_list'], type="json", auth="public", website="True")
def list(self, **kw):
result = request.env['product.product'].search([])
return request.render('my_theme.latest_products_list',{'values' : result })
i tried my best to understand how should i can continue here,
after a few search i concluded that i must send json request from javascript to this url route "/my_theme/ecommerce_list", and the controller will render my snippet template "my_theme.latest_products_list" that is declared in "addons/my_theme/views/templates.xml"
"i'm not sure is this the right way or there are other solutions that are easier than this"
this is my main confusion !!! really need your help to i understand more about odoo logic
so, i tried many JS codes and i'm done here
addons/my_theme\static\src\js\snippet.js
odoo.define('my_theme.snippet', function (require) {
'use strict';
var core = require('web.core');
var sAnimation = require('website.content.snippets.animation');
var _t = core._t;
var ajax = require(web.ajax);
sAnimation.registry.js_send_json_request = sAnimation.Class.extend({
function (require){
ajax.jsonRpc("/my_theme/ecommerce_list", 'call')
}
});
});
in my case
self.view.$el.append(html_from_server)
but does not working. TypeError: Cannot read property '$el' of undefined