Create Email (compose.js) - how do I access the pre-populated contacts from the render function?

Hello, 

For context: I am attempting to extend compose.js to set up some default values based on the number of contacts initially added. I am creating the email from the email subpanel on a case, which adds the case contacts into the email recipients by default.

The following code sample illustrates my problem: 

({
    extendsFrom: 'BaseEmailsComposeView',

    initialize: function (options) {
        this._super("initialize", [options]);
    },

    _render: function () {
        this._super("_render");

        // These lines show as 0 in the console.
        console.log(this.model.getRelatedCollection('contacts').length);
        console.log(this.model.get('to_addresses').length);
    }
})

The console output always shows 0 regardless of how many recipients. How can I retrieve the initial recipients? 

Thanks very much,

Martin

  • Hi Martin,

    You could try, instead of using the function render, in the initialize function call another function like this:

    initialize: function(options) {
        this._super('initialize', [options]);
        this.model.on('sync', this.otherFunction, this);

    },

    otherFunction: function() {
    console.log(this.model.getRelatedCollection('contacts').length);
            console.log(this.model.get('to_addresses').length)
    }

    I dont know if this can work but you can try.

  • Starting in 7.8 the render happens before the actual data is retrieved.

    You may have noticed that the fields are first displayed blank, and then populated with data:

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.8/Introduction/Migration_Guide/#Extran… 

    This means that your data is not yet available in the render method.

    To ensure that your code executes once the data is available, use an event that will execute after the data is synced, by adding the event in your initialize function:

    ({
        extendsFrom: 'BaseEmailsComposeView',

        initialize: function (options) {
            this._super("initialize", [options]);
            this.model.once('sync', function() {
              this.countContacts();
            }, this);
        },

        _render: function () {
            this._super("_render");
        },
        countContacts: function(){
            console.log(this.model.getRelatedCollection('contacts').length);
            console.log(this.model.get('to_addresses').length);
        }
    })

     

    HTH

    FrancescaS

  • Hello Francesca, Hello Salvador, 

    Thanks very much for your responses. 

    I've tried both with no luck unfortunately. It looks like the 'sync' event is never fired having put this in my initialize function: 

    this.model.on('all', function (event) {
        console.log(event);
    })

    Could it be something unique about BaseEmailsComposeView? I know its a special case because Email is a backward compatibility mode module. 

    Any other thoughts are much appreciated. 

  • See if you find some alternative ways of achieving what you need by reading this thread. It has a long discussion on customizing the compose. I shared a lot of the code I use in this thread:

    How can I open an email draft in a compose drawer? 

    FrancescaS

  • Thanks both for your help. 

    In the end I chose to put a listener on the 'change:to_addresses' event and perform my updates based on that. This works perfectly fine for my case as i'm only dependant on the values in the to_addresses list.