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?


  • 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
Reply
  • 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
Children
No Data