How to update options after sync in Javascript?

Hi,

we have a multiselect with some deprecated values. The deprecated values should be removed if not in saved values. This is what I tried in the module record..js:

    _filterLocations: function()
    {
          var location_c = this.model.get('location_c'), skipList = ['dus6.2'], i = 0;

          if (typeof location_c != 'undefined') {
              var list = app.lang.getAppListStrings('data_center_list');
             Object.keys(list).forEach(function(key) {
                  for (i in skipList) {
                       if (key == skipList[i] && ! location_c.includes(key)) {
                            delete list[key]
                       }
                  }
             })
             this.model.fields['location_c'].options = list   
          }
    }

The function is called by sync: this.model.once('sync', this._filterLocations, this)  

With console.log(list) is 'dus6.2' removed, but not in the page view.

How can I update / re-render the select values?

We are using SugarCRM 7.9

Regards

Harald

  • Hi Harald Kampen 

    I would create a custom enum field (custom/clients/base/fields/enum/), override the method loadEnumOptions in order to inject into self.items (the options returned from app_list_strings) the option fetched from database if it is not on app_list_strings anymore. This can be accomplished inside the "success" event of app.api.enumOptions.

    This way you can remove from app_list_strings the obsolete options. This strategy work for all enum fields so you don't need to worry about customizing every record.js/create.js.

    Regards

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Hi André,

    that's the other option but does not change the main problem that I have the values are loaded after rendering.

    Using this does not have the value location_c, and I don't know if I could use the sync event in this case to render again after loading the bean:

    ({
         extendsFrom: 'EnumField',

         initialize: function (options) {
              this._super('initialize', [options])
              this.type = 'enum'
         },
         
         loadEnumOptions: function (fetch, callback) {
              var self = this,
              location_c = this.model.get('location_c'),
              skipList = ['dus6.2'],
              i,
              list = app.lang.getAppListStrings('data_center_list');

              if (typeof location_c == 'undefined') location_c = [];

              Object.keys(list).forEach(function(key) {
                   for (i in skipList) {
                        if (key == skipList[i] && ! location_c.includes(key)) {
                             delete list[key]
                        }
                   }
              })

            self.items = list
            callback.call(self);
         }
    })

    Regards

    Harald

  • EvilPeri EvilPeri gave me a solution here some years ago that has been working for me, but I have multiple dropdown lists defined and switch which one is used. It may not be what you are looking for but it may give you some ideas:
    SugarCRM 7  How can I re-render a multi enum? 

    There is a modification for 7.6 in the thread as well as a solution by Pedro Bazan at the end of the thread.

  • Hi,

    this._render() works. I found a different solution. Cause the callback function is not available on the sync event, I save it in the field object.

    ({
         extendsFrom: 'EnumField',
         
         callback: null,

         initialize: function (options) {
              this._super('initialize', [options])
              this.type = 'enum'
              this.model.on('sync', this.loadEnumOptions, this) 
         },
         
         loadEnumOptions: function (fetch, callback) {
              var self = this,
              location_c = this.model.get('location_c'),
              skipList = ['dus6.2'],
              i,
              list = app.lang.getAppListStrings('data_center_list');

              if (typeof callback == 'function') this.callback = callback

              if (typeof location_c == 'undefined') location_c = [];

              Object.keys(list).forEach(function(key) {
                   for (i in skipList) {
                        if (key == skipList[i] && ! location_c.includes(key)) {
                             delete list[key]
                        }
                   }
              })

            this.items = list
            this.callback.call(this)
         }
    })