Announcement

Collapse
No announcement yet.

Updating Entities in Javascript vs PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Updating Entities in Javascript vs PHP

    I've added a custom button to the details view for one of our custom entities. This button updates various fields for items within the itemList of the parent Entity.

    My question is this: is it better to use Javascript to update the entity or is it better to call a PHP service action? Should this be a task primarily done in the back-end or does it actually not matter?

    Obviously, there are always many ways to skin the same cat but which is the preferred kosher way?

  • #2
    This is maybe controversial :-), I prefer to update in Javascript as much as possible and use Ajax to update "out of current domain" data (for example parent entity data), mostly to enhance the user experience (immediate reaction) and avoid having to reload the page to reflect changes made by a PHP service action.

    Comment


    • #3
      Hello,
      nice discussion, i think is simple :
      beforeSave = frontEnd => handler
      afterSaver = backEnd => formula

      And we need to think about acl/role.. sample :
      why put a button in front-end if currentUser/currentTeams have not "role" to use it ?

      So .. we need really have a good view where and how implement ... and this is i think the real response

      Regards

      Comment


      • #4
        Originally posted by item View Post
        And we need to think about acl/role.. sample :
        why put a button in front-end if currentUser/currentTeams have not "role" to use it ?
        Of course, if the user doesn't have the required permissions then the button will just not be visible.

        I'm still undecided. In my instance, the action which occurs when the button is clicked could potentially be run from many different views (for convenience) so for the sake of code duplication, etc, if there was just one back-end PHP service action, this could be triggered from the JS from any view.

        telecastg Once a PHP service action has been called, is it not possible to get the JS to force an view update by listening to the 'sync' of a model?

        Comment


        • #5
          Originally posted by blueprint View Post
          telecastg Once a PHP service action has been called, is it not possible to get the JS to force an view update by listening to the 'sync' of a model?
          I am still too green regarding Espo / Backbone display update facilities. Could you post some sample code of forcing a view update ?
          Last edited by telecastg; 01-27-2020, 09:21 PM.

          Comment


          • #6
            Originally posted by telecastg View Post

            I am still too green regarding Espo / Backbone display update facilities. Could you post some sample code of forcing a view update ?
            Likewise, as am I - I'm not sure which trigger to functions to call and why. The only ones I know of are:

            Code:
            this.model.trigger('update-all');
            and

            Code:
            this.model.trigger('sync');
            However I couldn't tell you the difference.

            Comment


            • #7
              Hello,
              if understand..and my 2cents skill.. for me this work :
              PHP Code:
              self.model.save(null, {
                                              
              error: function () {
                                                  
              console.log('error saving');
                                              }.
              bind(self)
                                          }); 
              the (record) view is updated in live..
              so .. need to update the relationShip view .. !?




              Comment


            • #8
              Originally posted by blueprint View Post
              I'm still undecided. In my instance, the action which occurs when the button is clicked could potentially be run from many different views (for convenience) so for the sake of code duplication, etc, if there was just one back-end PHP service action, this could be triggered from the JS from any view.
              An alternative to avoid front-end code duplication and improve functionality organization could be to use custom libraries.

              For example, I created the script client/custom/lib/js/customApp.js and inside defined some custom functions that I use like:
              Code:
              // returns a javascript date object as ISO date string
              function [COLOR=#FF0000]ISODateString(d)[/COLOR]{
                  function pad(n){return n<10 ? '0'+n : n;}
                  return d.getFullYear()+'-'+ pad(d.getMonth()+1)+'-'+ pad(d.getDate())+' '+ pad(d.getHours())+':'+ pad(d.getMinutes())+':'+ pad(d.getSeconds());
              }
              Then registered the script in the in the client custom scripts metadata file: custom/Espo/Custom/Resources/metadata/app/custom.json
              Code:
              {
               "scriptList": [
                     "__APPEND__",
              [COLOR=#FF0000]"client/custom/lib/js/customApp.js"[/COLOR],
                    "client/custom/lib/jqueryResizableColumns/jquery.resizableColumns.js",
                    "client/custom/lib/js/custom-dynamic-logic.js",
                    "client/custom/lib/js/insQ.min.js"
              
               ],
                 "developerModeScriptList": [
                     "__APPEND__",
              [COLOR=#FF0000]"client/custom/lib/js/customApp.js"[/COLOR],
                    "client/custom/lib/jqueryResizableColumns/jquery.resizableColumns.js",
                    "client/custom/lib/js/custom-dynamic-logic.js",
                    "client/custom/lib/js/insQ.min.js"
                 ],
                  "cssList": [
                      "__APPEND__",
                     "client/custom/lib/css/customApp.css",
                     "client/custom/lib/jqueryResizableColumns/jquery.resizableColumns.css"                
                  ],
                  "developerModeCssList": [
                      "__APPEND__",
                     "client/custom/lib/css/customApp.css",
                     "client/custom/lib/jqueryResizableColumns/jquery.resizableColumns.css"                
                  ]  
              }
              Now I can call function ISODateString() from any javascript file like this:
              Code:
              var d = new Date();
              var timestamp = [COLOR=#FF0000]ISODateString(d);[/COLOR]
              Last edited by telecastg; 01-28-2020, 06:39 PM.

              Comment

              Working...
              X