Welcome Guest, Not a member yet? Register   Sign In
Problem with javascript and jquery class and the solution.
#1

[eluser]Adam Liszkai[/eluser]
Hi Developers!

I read many places that CodeIgniter javascript library does not currently viable. I looked for, looking in the code and I think I found a few hours after the hang of it. First of all, a place I had to write in the code:

/codeigniter/system/libaries/javascript/jquery.php in a rows 946-962

Original:
Code:
if (count($this->jquery_code_for_compile) == 0 )
  {
   // no inline references, let's just return
   return ;
  }

  // Inline references
  $script = '$(document).ready(function() {' . "\n";
  $script .= implode('', $this->jquery_code_for_compile);
  $script .= '});';
  
  $output = ($script_tags === FALSE) ? $script : $this->inline($script);

  $this->CI->load->vars(array($view_var => $output));
}

And the correction:
Code:
if (count($this->jquery_code_for_compile) == 0 )
  {
   // no inline references, let's just return
   return ;
  }

  // Inline references
  $script = '$(document).ready(function() {' . "\n";
  $script .= implode('', $this->jquery_code_for_compile);
  $script .= '});';
  
  $output = ($script_tags === FALSE) ? $script : $this->inline($script);

  $this->CI->load->vars(array($view_var => $output));

  return $script;
}
I added "return $script;" after "$this->CI->load->vars(array($view_var => $output));"

In my class the code looks:
Code:
/* ... more code, but not important here ... */
  
  $this->jquery->_document_ready('alert("Jquery class work!!");'."\n\r");
    
    $header['jquery_libaries'] = array(
      $this->jquery->script(JAVASCRIPTS.'jquery.min.js'),
      $this->jquery->script(JAVASCRIPTS.'jquery-ui.min.js'),
      $this->jquery->script(JAVASCRIPTS.'jquery.easing.min.js')
    );
  $header['script_head'] = $this->jquery->_compile();
    
  /* ... more code, but not important here ... */

  $this->load->view('header', $header);

  /* ... more code, but not important here ... */
I define JAVASCRIPTS to "http://localhost/liszkaiadam-hu/subdomains/jquery/" because in the near future i will use in my page and the javascripts have their own separate vhost.

My header view file:
Code:
/* ... more code, but not important here ... */

echo('<!-- $jquery_libaries -->'."\n\r");
foreach($jquery_libaries as $script)
{
  echo($script);
}

echo("\n\r");
echo('<!-- $script_head -->'."\n\r");
echo('< script type="text/javascript">'."\n\r");
echo($script_head."\n\r");
echo('</ script>'."\n\r");

/* ... more code, but not important here ... */

And the output is:
Code:
<!-- $jquery_libaries -->
< script type="text/javascript" charset="utf-8" src="http://lo.../jquery.min.js" ></ script>
< script type="text/javascript" charset="utf-8" src="http://lo.../jquery-ui.min.js"></ script>
< script type="text/javascript" charset="utf-8" src="http://lo.../jquery.easing.min.js"></ script>

<!-- $script_head -->
< script type="text/javascript">
$(document).ready(function() {
alert("Jquery class work!!");
});
</ script>
Here I hit a space is opening and closing the scrtipt tag to actually load as well as shortened the src :)

So I solved this problem for me, the javascript library automatically loaded in autoload config file, the jquery is loaded it automatically.

I apologize for my English, I never learned a books or from teachers.

Cheers!
#2

[eluser]Unknown[/eluser]
I think there is no need of any modification to the CI Libraries.

The way to load the library is as following:

1) load the library javascript library in your model constructor
Code:
class Example extends CI_Controller {
//constructor public function __construct() {
    parent::__construct(); // loading appointment model to handle database operations
$this->load->model('example/Admin', 'admin'); $this->load->library('javascript'); }

2) Add the event
Code:
$this->javascript->click('#button', "alert('Hello!');");
$this->javascript->compile();

3) Get the loaded variable and send them to your view
Code:
$data['library_src'] = $this->load->get_var('library_src');
$data['script_head'] = $this->load->get_var('script_foot');

4) Grab & echo the variables in Header / View
Code:
<?php echo $library_src;?>
<?php echo $script_head;?>

Happy jQuerying, It works.

5) Output for the above looks like this in view-source
Code:
<s.cript type="text/javascript" charset="utf-8" src="http://gizmo.com.au:8002/ci/themes/basic/js/jquery/jquery.js"></s.cript>
        <s.cript type="text/javascript" charset="utf-8">
// &lt;![CDATA[
$(document).ready(function() {

$("#button").click(function(){
  alert('Hello!');
  return false;
});
});
// ]]>
</s.cript>

Note: </s.cript>, thats because it should not be removed from the comment Confusednake:




Theme © iAndrew 2016 - Forum software by © MyBB