model is undefined on document ready

  • 1
  • Question
  • Updated 4 weeks ago
  • Answered
  • (Edited)
I am trying to get the current loaded model id in my document ready method.
(function(skuid) {
    var $ = skuid.$;
    var params = arguments[0];
    var activateHandler = function() {
        skuid.$("#bodyTable .nx-conditioncontainer-left .nx-conditiontoken.nx-actionselect .nx-actionselect-arrow").on("click", function() {
            setTimeout(function() {
                skuid.$("#bodyTable .nx-conditioncontainer-left .nx-actionselect-dropdown .nx-actionselect-dropdown-item").on("click", function() {
                    if (this.innerHTML === '** All Years')
                    {
                        console.log('All Years');
                        console.log(params.model.id);
                    }
                });
            })
        });
    };
    $(document.body).one('pageload', activateHandler);
    skuid.events.subscribe("models.loaded", activateHandler);
})(skuid);
Photo of Prateek Kumar

Prateek Kumar

  • 276 Points 250 badge 2x thumb

Posted 4 weeks ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 53,702 Points 50k badge 2x thumb
Hi Prateek, "params.model.id" is the issue here, params.model will never be defined in this scenario. Since you are already hard-coding the name of the table element (#bodyTable), you could just hard-code the name of the Model Id as well, and access it using skuid.model.getModel(modelId):

console.log('All Years');
var myModelId = "AllYearsModel";
var myModel = skuid.model.getModel(myModelId);
console.log(myModel.id);

If you are trying to get the id of the model dynamically from the DOM, you could access it from a table instance like this:

var tableModel = skuid.$("#bodyTable").data("object").model;
console.log("Table's model id: " + tableModel.id");
Photo of Prateek Kumar

Prateek Kumar

  • 276 Points 250 badge 2x thumb
Hi Zach,
I tied to get the modal id dynamically but I am still getting the error as undefined. The use case of this logic is to after executing the below line code
$(document.body).one
I want to get the currently loaded model id dynamically, so that I can modify something with the currently loaded model, not with the other tabsets.

Please let me know your expert suggestions to solve this issue.
Photo of Zach McElrath

Zach McElrath, Employee

  • 53,702 Points 50k badge 2x thumb
Prateek, I am not sure what you mean by "currently loaded" model. I would modify your code to something like this:

(function(skuid) {
    var $ = skuid.$;
    var activateHandler = function() {
        skuid.$("#bodyTable .nx-conditioncontainer-left .nx-conditiontoken.nx-actionselect .nx-actionselect-arrow").on("click", function() {
            setTimeout(function() {
                skuid.$("#bodyTable .nx-conditioncontainer-left .nx-actionselect-dropdown .nx-actionselect-dropdown-item").on("click", function() {
                    if (this.innerHTML === '** All Years')
                    {
                        console.log('All Years');
                        var modelId = $(this).closest(".nx-skootable").data("object").model.id
                        console.log("Model Id: " + modelId);
                    }
                });
            })
        });
    };
    $(document.body).one('pageload', activateHandler);
    skuid.events.subscribe("models.loaded", activateHandler);
})(skuid);
(Edited)
Photo of Prateek Kumar

Prateek Kumar

  • 276 Points 250 badge 2x thumb
Thanks a lot Zach, it worked for me.