Announcement

Collapse
No announcement yet.

Model is not being fetched completely in setup function of record/detail.js

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Model is not being fetched completely in setup function of record/detail.js

    Hi,

    I got a completely weird problem, I never faced before. i.e model shows email, and a few other fields as undefined for contact record, and when I refresh the page it starts showing me data.

    My aim to add a custom button in detail layout of contact if that record meets condition.

    in js for contact/record/detail I wrote following lines

    setup: function(){
    Dep.prototype.setup.call(this);
    if(!_.isEmpty(this.model.get("cVId"))){
    this.buttonList.push({
    name: "submitCandidate",
    label: this.translate('submitCandidate', 'labels', 'Contact'),
    action: "submitCandidate",
    style: "warning"
    });
    }
    },


    this.model.get("cVId") contains a value but it doesn't add button in first run, I have to reload page every time i visit contact detail page from list view.

    Please advise if there is any change regarding model fetching in setup in new releases or anything is wrong with my code.

    Thanks
    Devs Crew

  • #2
    Hi,

    I looks like the model is not fully loaded at the moment when the function is called.

    Try this:
    Code:
    setup: function(){
        Dep.prototype.setup.call(this);
        this.listenToOnce(this.model, 'sync', function () {
            if(!_.isEmpty(this.model.get("cVId"))){
                this.buttonList.push({
                    name: "submitCandidate",
                    label: this.translate('submitCandidate', 'labels', 'Contact'),
                    action: "submitCandidate",
                    style: "warning"
                });
            }
        }, this);
    },

    Comment


    • #3
      telecastg thank you for quick response.

      Yes, exactly that is the problem, but
      firstly it should load model in setup, it used that to many other instances in past.
      secondly that sync didn't work, it waited and executed the inner function but setup didn't show the button I wanted. due to js asynchronous behaviour.

      Comment


      • #4
        You're welcome devscrew

        What I usually do, when coding ajax calls and want to avoid the asynchronous behavior, is to "chain" instructions until after the ajax response has been received but since in this case you are not making an express ajax call, it looks like the behavior has to do with Backbone's internal ajax workings.

        I think this might help then: https://docs.espocrm.com/development...fore-rendering
        Last edited by telecastg; 07-14-2020, 11:34 PM.

        Comment


        • #5
          Exactly, I tried wait earlier and again after reading the document you've mentioned, but still no luck.. some time it adds a button, sometime it doesn't... seems like something is wrong with the internal backbone.js version for this Espocrm version (i.e 5.7.5)

          Comment


          • #6
            I would upgrade to the current version, just in case.

            Also might want to try to add the button "after render" ?. I have also done some coding using jquery to "force" my way into a fully rendered display, but of course this would not address the reason for the strange behavior.
            Last edited by telecastg; 07-16-2020, 04:01 PM.

            Comment


            • #7
              Yeah I chose that too, upgraded instance still no luck..:-(

              Comment

              Working...
              X