page include data('object') issues

  • 1
  • Question
  • Updated 3 years ago
  • Answered
I have used this paradigm many times to access a page title and display messages:
var pageTitle = $('#ContractErrors');	
var editor = pageTitle.data('object').editor;
editor.handleMessages([{message: 'Pizza is good!', severity:'ERROR'}]);
I am trying to use this code inside of a page include. When I hit the second line, in the page include, I am getting an error that 'Cannot read property "editor" of undefined'. Obviously the pageTitle variable is missing something when it's running in a page include. The page works perfectly when it's running by itself. Any ideas why this would happen? I have checked that there are no naming conflicts between the pages. Any ideas?
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Since this is a Page Include, I'd guess that the problem is that the Page Title hasn't been rendered to the page yet when your code runs. This code should be run within a block like this:

$(document.body).one('pageload',function(){
    var pageTitle = $('#ContractErrors'); 
    var editor = pageTitle.data('object').editor;
    editor.handleMessages([{message: 'Pizza is good!', severity:'ERROR'}]);
});

That should ensure that your Page Title has been rendered. If your Page Title hasn't been rendered yet, then .data('object') will return undefined, because the jQuery selector $('#ContractErrors') will return an empty array of elements.
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Thanks for the quick response zach! The thing is that I'm running this in a regular in-line snippet, however it is inside of a wizard which is inside of a page include. Basically the snippet runs between step3 and step4 of a wizard. I actually am not getting an empty array returned, more like an element that's missing something...  
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
This is what the pageTitle variable looks like:

Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
If the Page Title element is in the DOM, then you should be able to do this:

var pageTitle = $('#ContractErrors');
console.log(pageTitle.length);

and get 1 returned in the console
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
It's really weird because I'm getting 0 in the console, but I'm 100% sure that it's in the DOM. It works fine when it's not in the page Include...
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Sorry it was my bad, I'll never doubt skuid again...

I had a line that closed popups, which ended up closing the popup that the page include was in which made its DOM disappear. Thanks anyway!
(Edited)