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

Parents
  • 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

Reply
  • 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

Children
No Data