2

Here is my previous question. Now i created a do_delete method which extends the original one. I also included this into views.xml

<template id="assets_backend" name="amgl assets" inherit_id="web.assets_backend">
    <xpath expr="." position="inside">
        <script type="text/javascript"
                src="/amgl/static/src/js/amgl.js">
        </script>
    </xpath>
</template>

But the issue i am facing is that. It only works when i enable debug=assets and i just recently discovered that it doesn't even work with normal debug=1 mode.

Edit

amgl.js

function join_name(names) {
var str = "";
var step;
for (step = 0; step < names.length; step++) {
    str = str + (step + 1) + '- ' + names[step].full_name + ' \n';
};
return str;
}

odoo.define('amgl.web.ListView', function (require) {
"use strict";

var core = require('web.core');
var _t = core._t;
var Model = require('web.DataModel');
var ListView = require('web.ListView');
var ListViewDeleteExtension = ListView.include({

    do_delete: function (ids) {
        if (this.model == 'amgl.customer') {
            var self = this;
            new Model(self.model).call('read', [ids, ['full_name'], this.dataset.get_context()])
            .done(function (names) {
                var text = _t("Do you really want to remove these records?") + ' \n \n' + join_name(names)
                if (!(ids.length && confirm(text))) {
                    return;
                }
                return $.when(self.dataset.unlink(ids)).done(function () {
                    _(ids).each(function (id) {
                        self.records.remove(self.records.get(id));
                    });
                    // Hide the table if there is no more record in the dataset
                    if (self.display_nocontent_helper()) {
                        self.no_result();
                    } else {
                        if (self.records.length && self.current_min === 1) {
                            // Reload the list view if we delete all the records of the first page
                            self.reload();
                        } else if (self.records.length && self.dataset.size() > 0) {
                            // Load previous page if the current one is empty
                            self.pager.previous();
                        }
                        // Reload the list view if we are not on the last page
                        if (self.current_min + self._limit - 1 < self.dataset.size()) {
                            self.reload();
                        }
                    }
                    self.update_pager(self.dataset);
                    self.compute_aggregates();
                });
            });
        }
        else {
            if (!(ids.length && confirm(_t("Do you really want to remove these records?")))) {
                return;
            }
            var self = this;
            return $.when(this.dataset.unlink(ids)).done(function () {
                _(ids).each(function (id) {
                    self.records.remove(self.records.get(id));
                });
                // Hide the table if there is no more record in the dataset
                if (self.display_nocontent_helper()) {
                    self.no_result();
                } else {
                    if (self.records.length && self.current_min === 1) {
                        // Reload the list view if we delete all the records of the first page
                        self.reload();
                    } else if (self.records.length && self.dataset.size() > 0) {
                        // Load previous page if the current one is empty
                        self.pager.previous();
                    }
                    // Reload the list view if we are not on the last page
                    if (self.current_min + self._limit - 1 < self.dataset.size()) {
                        self.reload();
                    }
                }
                self.update_pager(self.dataset);
                self.compute_aggregates();
            });
        }
    },
});

});

Edit 2

This my complete xml file which i am including in manifest.

<odoo>
<data>
    <template id="assets_backend" name="amgl assets" inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script type="text/javascript"
            src="/amgl/static/src/js/amgl.js">
            </script>
        </xpath>
    </template>

    <template id="assets_backend1"
    name="web_duplicate_visibility backend assets"
    inherit_id="web.assets_backend">
        <xpath expr="."
        position="inside">
            <script type="text/javascript"
            src="/amgl/static/src/js/duplicate_visibility.js">
            </script>
        </xpath>
    </template>
</data>

Ancient
  • 3,007
  • 14
  • 55
  • 104

1 Answers1

2

In order for this to work you must extend the class that is saved in core.view_registry:

function join_name(names) {
var str = "";
var step;
for (step = 0; step < names.length; step++) {
    str = str + (step + 1) + '- ' + names[step].full_name+ ' \n';
};
return str;
}

odoo.define('amgl.web.ListView', function (require) {
    "use strict";

   // put an alert here and refresh the page if the alert don't appear
   // than the javascript file is not loaded at all you need to upgrade
   // you module 
   alert(" hi i'm loaded and my code is executed?!!!");

   var core = require('web.core');
var _t = core._t;
var Model = require('web.DataModel');
// the Class is saved in view_registry
// Note same thing for widgets,widgets for form view are saved in : form_widget_registry
// always look for the key and get them using that key
// in odoo 10.0 if you look at list_view.js you will find this line:
// core.view_registry.add('list', ListView);
// here he saved the class with key 'list'
var ListView = core.view_registry.get('list');
// just override the do_delete methd
ListView.include({
    do_delete: function (ids) {
        console.log('override the function don again'); // to make sure
        if (this.model == 'amgl.customer') {
            var self = this;
            new Model(self.model).call('read', [ids, ['full_name'], this.dataset.get_context()])
            .done(function (names) {
                var text = _t("Do you really want to remove these records?") + ' \n \n' + join_name(names)
                if (!(ids.length && confirm(text))) {
                    return;
                }
                return $.when(self.dataset.unlink(ids)).done(function () {
                    _(ids).each(function (id) {
                        self.records.remove(self.records.get(id));
                    });
                    // Hide the table if there is no more record in the dataset
                    if (self.display_nocontent_helper()) {
                        self.no_result();
                    } else {
                        if (self.records.length && self.current_min === 1) {
                            // Reload the list view if we delete all the records of the first page
                            self.reload();
                        } else if (self.records.length && self.dataset.size() > 0) {
                            // Load previous page if the current one is empty
                            self.pager.previous();
                        }
                        // Reload the list view if we are not on the last page
                        if (self.current_min + self._limit - 1 < self.dataset.size()) {
                            self.reload();
                        }
                    }
                    self.update_pager(self.dataset);
                    self.compute_aggregates();
                });
            });
        }
        else {
            if (!(ids.length && confirm(_t("Do you really want to remove these records?")))) {
                return;
            }
            var self = this;
            return $.when(this.dataset.unlink(ids)).done(function () {
                _(ids).each(function (id) {
                    self.records.remove(self.records.get(id));
                });
                // Hide the table if there is no more record in the dataset
                if (self.display_nocontent_helper()) {
                    self.no_result();
                } else {
                    if (self.records.length && self.current_min === 1) {
                        // Reload the list view if we delete all the records of the first page
                        self.reload();
                    } else if (self.records.length && self.dataset.size() > 0) {
                        // Load previous page if the current one is empty
                        self.pager.previous();
                    }
                    // Reload the list view if we are not on the last page
                    if (self.current_min + self._limit - 1 < self.dataset.size()) {
                        self.reload();
                    }
                }
                self.update_pager(self.dataset);
                self.compute_aggregates();
            });
        }
    },
});

});

always remember to load JS field by extending the backend_assets template:

 <!--We need to load our js file to backend_assets-->
        <template id="assets_backend" name="costum assets" inherit_id="web.assets_backend">
            <xpath expr="." position="inside">
                <script type="text/javascript" src="/amgl/static/src/js/amgl.js"></script>
            <script type="text/javascript"
        src="/amgl/static/src/js/duplicate_visibility.js">
        </script>
            </xpath>
        </template>

and add this file to manifest

'data': [
  'your_xml_file_name.xml',
],

and it worked for me the message is shown on the console. you must be doing something wrong

for testing purpose i changed the code to to fetch name instead of full name so it worked with sales team model as expected.

enter image description here

Charif DZ
  • 14,415
  • 3
  • 21
  • 40
  • its still same bro – Ancient Oct 30 '17 at 12:42
  • Did you add this file to backend assets and to the manifest ? Because it work can you past all your code – Charif DZ Oct 30 '17 at 12:48
  • sure, let me update my question with all of the code. – Ancient Oct 30 '17 at 12:54
  • infact, there is nothing more to update, the xml template in question is added in main `views.xml` file and that file is included in manifest. But adding file to backend assets. I don't know how to do this – Ancient Oct 30 '17 at 12:58
  • I was thinking that when i add template in `views.xml` and reference my customer js file. It will automatically be register – Ancient Oct 30 '17 at 12:58
  • using the js code and extending backend_assets worked for me and i can see the message in the console. this proves that the method is overrided – Charif DZ Oct 30 '17 at 14:20
  • I updated my question with my complete xml file can you look into this if i am making any mistake. Because there are only two files one js and xml and both are in question now. I don't know why its not working even when its working on your side – Ancient Oct 30 '17 at 17:46
  • you can see i have other js extension here `duplicate_visibility` and its working without debug mode – Ancient Oct 30 '17 at 17:51
  • first you don't need to create two template tags put them inside one and as i did in my edits put an alert message and refresh the page because odoo will call define only one time and if the the js file is put in the template correctly you should see the message. Note when you change javascript file you don't need to upgrade the module but if you change xml that are in data in the manifest you need to upgrade the module and make sure the ids are not the same in you xml because odoo will load only the last one – Charif DZ Oct 30 '17 at 18:11
  • Well, I just replaced xml file with your xml and it suddenly started working. Thanks a lot – Ancient Oct 30 '17 at 18:26
  • and the ids in the old xml were not the same in the templates? – Charif DZ Oct 30 '17 at 18:29
  • Yes, ids were not same in old template, everything was same. Even i tried to adding both files in one template as its now. But it didn't worked. All i guess is that it was cached and when we change the js file and added alert. It started working. Also i did tried updating module previously but still it was not working. – Ancient Oct 30 '17 at 18:37
  • another question did you close the odoo tag ? and check the full path is it correct? – Charif DZ Oct 30 '17 at 18:54
  • Yes, i did and paths were also correct. Can you please also have a look into this if you have time https://stackoverflow.com/questions/46893852/odoo-how-to-add-external-jquery-plugin – Ancient Oct 30 '17 at 18:58
  • i think what you asking in this question is very hard to do what i recommand for is to read odoo tutorialhttps://www.odoo.com/documentation/8.0/howtos/web.html#the-form-view-custom-widgets you complete it in one day but you will have a real idea about the javascript in odoo and how you can extend them – Charif DZ Oct 30 '17 at 19:09