Welcome Guest, Not a member yet? Register   Sign In
Loading Javascript once for all views?
#1

[eluser]mikerh[/eluser]
For years now I have been using my own home-brewed type of PHP framework but decided to switch over to CI.

In it's current form my application will load and instantiate a large javascript singleton that is used for the entire site for the duration of the session, across all "views".

One thing I am confused on is how I can accomplish this in CI. For simplicity sake assume the application is a site with "home" and "about" views, home being the default controller. When home is called it loads the home view, all the js, and instantiates my js singleton. When the about view is then loaded I don't want to reload all the js and it needs to be able to interact with the already instantiated js code.

Is there a way to load all of this globally so that all views can use it?

Thanks
#2

[eluser]CroNiX[/eluser]
Sounds like you need to break your view into 2 (or more) views.

The first view would be the header, which would contain everything in the <head> of your document. This would get loaded on all pages. It would probably be dynamic so you can pass the page title to it.

The other view would be your content, or everything within the <body>. You can create additional views for footers, menus, etc.

Then I'd create a library or a helper, so all you have to do is pass it the page title and "current" page data and it will automatically output the header and then the content to form a complete page.

Here's a simple helper...

Code:
function create_page($title = 'page title', $content = '')
{
  $CI =& get_instance();  //get CI superobject
  
  //grab the header view sending the page title to it
  $header_data['page_title'] = $title;
  $header = $CI->load->view('template/header', $header_data, TRUE);

  //now just send the header and content to CI's output class (so we can use caching if we want)
  $CI->output->set_output($header . $content);
}

When you're in a controller, now all you have to do is pass the function the page title, and the rendered view of your content by using the 3rd parameter of the load::view() method.

Code:
$view_data['some_var'] = 'something';
$current_view = $this->load->view('some_view', $view_data, TRUE);

//now send the page title and the rendered content view to the helper function...
create_page('my page title', $current_view);
#3

[eluser]NeoArc[/eluser]
You may want to use a javascript module loader, like require.js
#4

[eluser]mikerh[/eluser]
Thanks for the feedback but not exactly what I was thinking. Even if I separate out the views I still have to load the header which is going to reload all the js and re-instantiate. Hmmm...have to think on this one.
#5

[eluser]CroNiX[/eluser]
Sure, but each page only has one header...and the helper I showed you will output it automatically on each page if you use it the way I showed.
#6

[eluser]mikerh[/eluser]
Correct...but...if for example I create a var in javascript in the home view I wont be able to use it in the about view as that was part of the $(document).ready() function in the home view when it first loaded the header. If I load the header again in the about view it will have another $(document).ready() function.
#7

[eluser]CroNiX[/eluser]
Look into an asset manager. You can dynamically add js/css files and raw js/css from your controller, which it will stick in the head of the document...

I load jQuery and a few other things globally, and then add specific files as needed per controller, like your custom scripts.




Theme © iAndrew 2016 - Forum software by © MyBB