CodeIgniter Forums
[SOLVED]Hook Question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: [SOLVED]Hook Question (/thread-44114.html)



[SOLVED]Hook Question - El Forum - 08-03-2011

[eluser]jimmygle[/eluser]
Here's my scenario -- can anyone give me some guidance?

I have a view that outputs a table and it's fed some data. I'm trying to throw some logic via a hook or whatever other means that will check for that view being loaded and if it is being loaded, send an additional parameter to the footer to load the associated Javascript.

Basically, anytime this HTML table view loads, I want the DataTables Javascript to load in the footer.

I hope this makes sense -- any help would be greatly appreciated.

Thanks in advance.


[SOLVED]Hook Question - El Forum - 08-03-2011

[eluser]idealws[/eluser]
Sounds like your loading the data via your controller at least you should be. And your more than likely using a conditional statement such as:

Code:
if($this){
    $this->load->view('yourview');
}else{
    $this->load->view('yourview');
}

If this is the case why not try adding variable in the condition like so:

Code:
if($this){
    $data['tablewasloaded'] = TRUE;
    $this->load->vars($data);
    $this->load->view('yourview');
}else{
    $data['tablewasloaded'] = FALSE;
    $this->load->vars($data);
    $this->load->view('yourview');
}

In your footer view:
Code:
if($tablewasloaded){
    // Show something here
}

Kind of a quick example but should get your started.


[SOLVED]Hook Question - El Forum - 08-03-2011

[eluser]jimmygle[/eluser]
What you proposed is what I'm doing at the moment. But that's making me have to load the "table" view AND tell the footer to load the corresponding Javascript.

I'm trying to make it so when I load the "table" view, the footer view somehow automatically detects that the table view was loaded and then proceeds to load the corresponding javascript automatically.


[SOLVED]Hook Question - El Forum - 08-03-2011

[eluser]Mirge[/eluser]
First thing that comes to mind is within your table view, doing something along the lines of:

(EDIT: this goes in your table view)
Code:
$this->_loadDataTables = TRUE;

And within your footer,

Code:
<?php if($this->_loadDataTables): ?>
script markup here
<?php $this->_loadDataTables = FALSE; endif; ?>

Maybe not the most elegant, but I imagine it'd work fine? Untested.


[SOLVED]Hook Question - El Forum - 08-03-2011

[eluser]jimmygle[/eluser]
Wow! I'm beating myself over the head with the KISS stick.

Thanks, Mirge. That worked perfect.


[SOLVED]Hook Question - El Forum - 08-03-2011

[eluser]Mirge[/eluser]
Haha, happy to help.


[SOLVED]Hook Question - El Forum - 08-04-2011

[eluser]Aken[/eluser]
I'd recommend placing as much of your Javascript in a single file, so it will be cached and ready before you even get to that page, and you won't need to use any additional code to flag its appearance in your footer.