Waiting to execute javascript once page is fully loaded in Sugar 7

I need to hide a button in record view depending on the status

The problem I have is that the page needs to be fully loaded before I run the javascript to hide it

How can I do this?

I tried $( document ).ready(function() { but that doesnt work

Any ideas will be gratefull received

Sugar 7 is beginning to seriously depress me :(




Parents
  • Thanks

    I tried :

    $(document).on('load', function() {alert(1)})         
    $(window).load(function() {alert(2)})

    neither are triggered :(

    I think I may need to use $.when( to check if page is loaded but can't work out how at the moment

    This is incredibly frustrating 

    Is it only me that is finding doing the simplest things in sugar 7 is really difficult?

    I asked someone from Sugar and was pointed to the Adding Field Validation document but that is worse than useless for what I'm trying todo



  • Enrico

    I agree it is very useful for validation but it is not working for what I am trying todo which is hide a button depending on a value.

    Once I have got it working I will expand it to hide fields as well

    I tried adapting your code as follows

      myCustomLogicOn: function()    {
            
            if(this.model.get('phone_office') == '7777')
            {
                // log something on the console
                console.log('executing  my custom action');
                $('[name="validate_postal_code"]').hide();
              
            }
        },


    The function is executed but the button is not hidden

    I suspect it is running before the page is fully loaded

    I have proved that the code  $('[name="validate_postal_code"]').hide(); works by adding it to the click of another button 

    Am I missing something?


Reply
  • Enrico

    I agree it is very useful for validation but it is not working for what I am trying todo which is hide a button depending on a value.

    Once I have got it working I will expand it to hide fields as well

    I tried adapting your code as follows

      myCustomLogicOn: function()    {
            
            if(this.model.get('phone_office') == '7777')
            {
                // log something on the console
                console.log('executing  my custom action');
                $('[name="validate_postal_code"]').hide();
              
            }
        },


    The function is executed but the button is not hidden

    I suspect it is running before the page is fully loaded

    I have proved that the code  $('[name="validate_postal_code"]').hide(); works by adding it to the click of another button 

    Am I missing something?


Children
  • Hi Mike,    It seems that what you are trying to do might be better approached using the Sugar Logic on Field Dependency.  You can configure this through studio by defining a field (e.g. Cases Description) to be Visible if equal($priority, "P1").  This will hide the Description field automatically if you set the Priority to "High".    If you do need a more complex rules, you can use the following snippet (based on Enrico's sample code):    ({      extendsFrom: 'RecordView',        initialize: function(options)      {          this._super('initialize', [options]);      },        _render: function()      {          this.myCustomLogicOnCases();          this._super('_render');      },        myCustomLogicOnCases: function()      {          // when case priority is High...          if(this.model.get('priority') == 'P1')          {              var thepanel = '';              var thefield = '';              _.each(this.meta.panels, function(pn, idx_pn) {                  if(thepanel == '')                  {                          _.find(pn.fields, function(fld, idx_fld) {                                  if(fld.name == 'description') {                                          thefield = idx_fld;                                          return true;                                  }                          });                          if(thefield != '') {                                  thepanel = idx_pn;                          }                  }              });                if(thefield != '' && thepanel != '')              {                  // remove the meta                  this.meta.panels[thepanel].fields.splice(thefield, 1);              }          }      },        _dispose: function()      {          this._super('_dispose');      }  })      Basically, the field will be hidden by removing the fields metadata completely before rendering.  Note that you might have to handle when the fields change since this will only work when you first load the Case Record view.    Hope this helps,  Felix  InisightfulCRM Australia
  • Hi Mike,    One way to do this is to manipulate the metadata before rendering the Record View.  You can remove the buttons you want to hide before rendering.      Using Enrico's sample code, in the example below, the "Share" button will be hidden in Cases Record View if the Priority is set to High:    ({      extendsFrom: 'RecordView',        initialize: function(options)      {          this._super('initialize', [options]);      },        _render: function()      {          this.myCustomLogicOnCases();          this._super('_render');      },        myCustomLogicOnCases: function()      {          // when case priority is High...          if(this.model.get('priority') == 'P1')          {              var thebutton = '';              var theidx = '';              // find the buttons index for the share button              _.find(this.meta.buttons, function(bn, idx) {                  if(bn.name == 'main_dropdown') {                          thebutton = idx;                          _.find(bn.buttons, function(bbn, idx_bbn) {                                  if(bbn.name == 'share') {                                          theidx = idx_bbn;                                          return true;                                  }                          });                          return true;                  }              });                if(thebutton != '' && theidx != '')              {                  //remove the meta                  this.meta.buttons[thebutton].buttons.splice(theidx, 1);              }          }      },        _dispose: function()      {          this._super('_dispose');      }  })    Hope this helps.    Cheers,  Felix  InsightfulCRM Australia
  • Felix

    Thanks that works to delete a button

    Really helpful

    I'd still like to know if there's a way to run the JavaScript when the page is fully loaded as hiding the button was an example

    I also need to add buttons using JavaScript, hide fields and other things

    Any ideas?


  • Were you able to hide fields on load?
    I'd be interested in how you did it....

    thanks,
    FrancescaS
  • Got it, the code that Enrico put up helped A LOT.
    The thing is that on my 7.2 if I use the underscore on the render function
     
    _render: function()

     it won't work (I get a blank detail section and just the subpanels showing, no errors on the console) while if I just use

    render: function()

    it works fine.
    I do wonder what the difference really is....