Accessing Model without component

  • 1
  • Problem
  • Updated 5 years ago
  • Solved
Is this possible? In a static resource JS file, when I try something like this:


var taskModel = skuid.model.getModel('MimeoTask');
var task = taskModel.getFirstRow();

I get the error:

Uncaught TypeError: Cannot call method 'getFirstRow' of undefined

Obviously this code is executing before the model has loaded up, so is there a callback or hook I can attach my code to? I know I can get it to work if I wrap it in a component. Just wondering if it is possible to do without a component, and why there is no documentation of this?

The example at the bottom of the Model API reference: http://help.skuidify.com/s/tutorials/... makes it seem like this should work.
Photo of Charlie Jonas

Charlie Jonas

  • 670 Points 500 badge 2x thumb
  • frustrated

Posted 5 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Good question, and you're right, we should add documentation to help describe this.

The answer is, yes it is possible to do without a component.

The trick is to run this in a jQuery ready callback so that it won't get run until all Models have been loaded, and all Components on the page have been built.

You can do this by adjusting your JS code to be something like this:



(function(skuid){
skuid.$(function(){
var taskModel = skuid.model.getModel('MimeoTask');
var task = taskModel.getFirstRow();
});
})(skuid);



Really you could just do this:




skuid.$(function(){
var taskModel = skuid.model.getModel('MimeoTask');
var task = taskModel.getFirstRow();
});




but we wrap the code in a closure just to make sure there are no conflicts with global variables.

skuid.$ is just a reference to the jQuery version that Skuid uses.
Photo of Charlie Jonas

Charlie Jonas

  • 670 Points 500 badge 2x thumb
I noticed that it worked if I put it inside the $.ready callback, but I wasn't sure if this was a timing thing and would be unreliable or if the model was guaranteed to be available at this point.

Thanks for the quick reply.