Welcome Guest, Not a member yet? Register   Sign In
Calling helpers from within external JS
#1

[eluser]jpeffer[/eluser]
I was wondering if anyone could help explain how I can call a CI helper from within an external JS file. I'm able to call php from within the file but do not have a reference to CI or the autoloaded helpers that work fine within the view referencing the JS.

Thanks for the help.
#2

[eluser]Cadu de Castro Alves[/eluser]
What do you really want to do?
#3

[eluser]jpeffer[/eluser]
I need to retrieve an array located within a rules configuration file, process the rules for JS formatting, and output the result into locations within the external JS file.
#4

[eluser]dirkr[/eluser]
[quote author="jpeffer" date="1203373821"]I was wondering if anyone could help explain how I can call a CI helper from within an external JS file. [/quote]

Unlike a view your external JS file will be loaded directly into the browser and will not be passed through the CI system where CI helpers could be parsed.
#5

[eluser]jpeffer[/eluser]
[quote author="dirkr" date="1203564765"][quote author="jpeffer" date="1203373821"]I was wondering if anyone could help explain how I can call a CI helper from within an external JS file. [/quote]

Unlike a view your external JS file will be loaded directly into the browser and will not be passed through the CI system where CI helpers could be parsed.[/quote]

Yea I have discovered this as well sadly. It however does make sense. Is there a way to parse the file and write the results to disk? This would allow me to generate the scripts at startup.

The motivation behind this is to define CI rules once and translate them into the JS JQuery validations I need. Any ideas?
#6

[eluser]Lone[/eluser]
A nice and easy (dirty?) way you could achieve this is create a function in a controller that loads a view that is really the js file but in php and then mask it with htaccess Smile

And because that made no sense heres a sample

In some controller ('manage' for this sample)
Code:
function js() {
  $this->load->view('../../js/myscript'); // of course the ../../ is dependant on your application folder location
}

Put all of your JS in the myscript.php file in the /js directory and then you have two options for loading it. You can do it raw like:
Code:
< script type="text/javascript" src="http://www.mysite.com/manage/js">

Or make it neater using htaccess..
Code:
// htaccess
RewriteRule js/myscript.js$ manage/js
// html
< script type="text/javascript" src="http://www.mysite.com/js/myscript.js">
#7

[eluser]dirkr[/eluser]
[quote author="Lone" date="1203567859"]A nice and easy (dirty?) way you could achieve this is create a function in a controller that loads a view that is really the js file but in php and then mask it with htaccess Smile[/quote]

This works?

Inventive and oh so dirty - although not quite the way to qualify you for the medal of honor for Good Coding Practice, I'm afraid [shudder].
#8

[eluser]Nick Husher[/eluser]
What about:
Code:
// mycontroller.php
// requires: PHP5
class MyController extends Controller {
  function index() {
    $some_array = array('one','two','threefourfive');
    $data['javascript_vars'] = array('arbitraryDataArray'=>$some_array);
    
    $data['javascript_vars'] = json_encode($data['javascript_vars']);

    $this->load->view('someview');
  }
}

// someview.php
<html>
   <head>
     <-script->
      var pageValues = &lt;?=$javascript_vars ?&gt;
     </-script->
   &lt;/head&gt;
   &lt;body&gt;
   &lt;/body&gt;
&lt;/html&gt;

You can access your PHP-defined Javascript array with pageValues.arbitraryDataArray.

It's not the best way of doing it, but it imposes a clear separation between Javascript logic code and 'bridge data' that communicates between your PHP and your Javascript.




Theme © iAndrew 2016 - Forum software by © MyBB