model is undefined on document ready

  • 2
  • Problem
  • Updated 1 year ago
  • Solved
I'm running inline javascript on document ready which gets a model as follows:

$(function(){     
        var showTabsModel = skuid.$M('ShowTabs'),
            showTabsRow = showTabsModel.getFirstRow();

// do some other things...

};

I'm getting an error which indicates that showTabsModel returns undefined. Load Model data on page load is checked for ShowTabs, and the only condition on the model is based on userinfo.

Any idea why showTabsModel would be coming back undefined?
 
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb

Posted 4 years ago

  • 2
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Update: I'm running this page as a page include, from a queue.

When I run the page when I run the page as a stand-alone, everything works fine. When I run it as an include, i'm getting the javascript error (and a plank page).
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,470 Points 10k badge 2x thumb
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Like a charm! Thanks, Ben.
Photo of Dinesh Ramanadham

Dinesh Ramanadham

  • 372 Points 250 badge 2x thumb
@ben/ @Matt - I have the similar issue, but I am facing the problem in loading the model data from a button which is on the pageinclude page. tried $(document.body).one('pageload'), but page is not loading after added it. Any suggestions?
(Edited)
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Dinesh,

If you're loading a model from a button, you don't need the pageload function. Just set the actions for the button to include a query of your model.
Photo of Dinesh Ramanadham

Dinesh Ramanadham

  • 372 Points 250 badge 2x thumb
Thank you @matt for the reply. I am able to resolve the issue. but one strange thing i figured is, if I am querying model from other models save callback methods like

modelxyz.save({callback: function(response1){
if(!response1.totalsuccess) {
var modelabc = skuid.$M('modelabc');
}}});

in the above scenario variable modelabc is returning undefined. If I put the same statement outside save action it is working fine.

The above scenario is working if I am running included separately. Looks like I am missing something in this skuid approach. 

Thanks,
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Dinesh,

This may be a silly thing... but it's worth checking that the if statement is processing as you expect it to. Your "var modelabc = skuid.$M('modelabc');" line is only running if response1.totalsuccess is not true. You may want to just add
console.log(response1);
inside your if statement to make sure the if is working the way you expect.
Photo of Dinesh Ramanadham

Dinesh Ramanadham

  • 372 Points 250 badge 2x thumb
agreed Matt. The above code is just an example for the issue I am explaining. whatever model query statements that I am placing inside callback methods are returning undefined. all the remaining statements in that block are running fine. Anyway for now, I figured out the work around. But trying to understand why this is an issue.

Appreciate your time Matt. Thanks
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Nice. I generally have abandoned callback statements in favor of jquery $.when(), which seems to work more reliably.