Adding Custom JavaScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • caffeine
    Member
    • Aug 2014
    • 48

    Adding Custom JavaScript

    Hi Yuri,

    What's the correct way of adding custom JS libraries to the CRM?

    I edited jsLibs.json with my custom libraries but I don't know if I have to do something more.

    Thanks
  • yuri
    Member
    • Mar 2014
    • 8440

    #2
    Hi

    I think it's obvious if you ever encountered AMD.

    Metadata app.jsLibs

    "Flotr": {
    "path": "client/lib/flotr2.min.js",
    "exportsTo": "window",
    "exportsAs": "Flotr"
    }

    Then in javascript class

    PHP Code:
    Espo.define('Crm:Views.Dashlets.Abstract.Chart', ['Views.Dashlets.Abstract.Base', 'lib!Flotr'], function (Dep, Flotr) {
    
        return Dep.extend({
    
        });
    
    }); 
    
    The second argument of Espo.define function is the list of dependencies.

    It will load this js dynamically. exportsTo is where the lib exports its object. window.Flotr or $.summernote.
    Last edited by yuri; 10-24-2014, 01:37 PM.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • caffeine
      Member
      • Aug 2014
      • 48

      #3
      The JS define part was missing, thanks.

      Comment

      • Makov
        Junior Member
        • Jun 2016
        • 19

        #4
        Originally posted by yurikuzn

        Then in javascript class

        PHP Code:
        Espo.define('Crm:Views.Dashlets.Abstract.Chart', ['Views.Dashlets.Abstract.Base', 'lib!Flotr'], function (Dep, Flotr) {
        
        return Dep.extend({
        
        });
        
        }); 
        
        In what class? Class of added library? Excuse me, I'm junior developer, but i think that extended documentation on your product will be very helpful for the community.

        Comment

        • yuri
          Member
          • Mar 2014
          • 8440

          #5
          Crm:Views.Dashlets.Abstract.Chart corresponds to 'client/modules/crm/views/src/dashlets/abtract/chart' file

          But this definition style is dated.
          Now we use:

          crm:views/dashlets/abtract/chart => client/modules/crm/src/views/dashlets/abtract/chart


          custom:views/dashlets/abtract/chart => client/custom/srcviews/dashlets/abtract/chart
          If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

          Comment

          • Makov
            Junior Member
            • Jun 2016
            • 19

            #6
            Originally posted by yurikuzn
            Crm:Views.Dashlets.Abstract.Chart corresponds to 'client/modules/crm/views/src/dashlets/abtract/chart' file

            But this definition style is dated.
            Now we use:

            crm:views/dashlets/abtract/chart => client/modules/crm/src/views/dashlets/abtract/chart


            custom:views/dashlets/abtract/chart => client/custom/srcviews/dashlets/abtract/chart
            So, if I do that, it stops making my GET and POST requests with server-name/api/v1 prefix? Or am I doing something wrong? When I trying to do this
            Code:
            $('#address').suggestions{
            serviceUrl:"www.example.com";
            }
            it making request GET server-name/api/v1/www.example.com.
            Why??????????

            Comment

            • Makov
              Junior Member
              • Jun 2016
              • 19

              #7
              So, if I do that, it stops making my GET and POST requests with server-name/api/v1 prefix? Or am I doing something wrong? When I trying to do this
              Code:
              $('#address').suggestions{ serviceUrl:"www.example.com"; }
              it making request GET server-name/api/v1/www.example.com.
              Why??????????
              Another thing - i write my js script into the body of edit.tpl of address field.
              Problem still here.
              Why it making request adding site address with api path as prefix???

              Comment

              • yuri
                Member
                • Mar 2014
                • 8440

                #8
                Writing code in templates won't work properly. It's SPA, not a simple web page.

                You can write javascript only in classes. Which class to use depends on where you need to include your custom library.
                If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

                Comment

                • Makov
                  Junior Member
                  • Jun 2016
                  • 19

                  #9
                  Originally posted by yurikuzn
                  Writing code in templates won't work properly. It's SPA, not a simple web page.

                  You can write javascript only in classes. Which class to use depends on where you need to include your custom library.
                  Great thanks for helping!
                  So if I need to include my library and custom js into address field, which class would it be?

                  Comment

                  • yuri
                    Member
                    • Mar 2014
                    • 8440

                    #10
                    client/src/views/fields/address.js


                    A straightforward way.
                    method:
                    PHP Code:
                    setup: function () {
                      Dep.prototype.setup.call(this);
                      
                      this.wait(true);
                    
                      // here you load your lib asynchronically
                    
                      this.wait(false); // call this asynchronically after your lib loaded
                    }, 
                    
                    wait(); is used here to prevent rendering of the view before your lib is loaded.

                    Disable cache in Administration Settings for developing.
                    Last edited by yuri; 06-27-2016, 11:07 AM.
                    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

                    Comment

                    • Makov
                      Junior Member
                      • Jun 2016
                      • 19

                      #11
                      Thank you so much for directing me into the right way, but i did it a little bit my way. Anyway big up and thanks!

                      Comment

                      Working...