2013-07-14 2 views
4

매우 큰 형식이있는 레지스터 뷰 파일이 있습니다. 따라서 jquery 폼 유효성 검사 플러그인과 함께 폼 마법사 플러그인을 사용할 계획입니다. 이 스크립트를 특정 페이지에만로드하고 싶습니다. 어떻게해야합니까? 여기 내 컨트롤러 방법입니다. codeigniter로드보기 특정 자바 스크립트

public function index(){ 
    $data['title'] = "Register";   
    $this->load->view("site_header", $data); 
    $this->load->view("site_nav"); 
    $this->load->view("content_register"); 
    $this->load->view("site_footer"); 
} 

는 I에 유래 here에 유사한 게시물을 보았다 그러나 나는 무엇을 이해하지 않습니다. 도와주세요.

+0

[이 요지] (https://gist.github.com/qolami/5994689)가 도움이 될 수 있습니다. 이를 사용하여 외부 JavaScript 파일을 임베드하고 내부 JavaScript 문을 사용자 정의보기에 추가 할 수 있습니다. –

답변

9

변수로로드 할 j를 보낼 수 있습니다. 예제는 여기에 있습니다.

샘플 컨트롤러

class mypage extends CI_Controller{ 
    public function index(){ 
    $data['js_to_load']="index.js"; 
    $this->load->view('header',$data); 
    $this->load->view('my_html_page'); 
    } 

    public function second_page(){ 
    $data['js_to_load']='second.js'; 
    $this->load->view('header',$data); 
    $this->load->view('my_second_html_page'); 

    } 
} 

header.php -보기 일이 뭐죠

<!-- Needed html code here --> 

<? if ($js_to_load != '') : ?> 

<script type="text/javascript" src="assets/js/<?=$js_to_load;?>"> 

<? endif;?> 

<!-- Other html code here --> 

파일?

js 파일을 $ data 변수로 설정하고 $ data 변수를 헤더로 전달합니다. 헤더 파일에서 js_to_load이 설정되어 있는지 확인하고 있습니까? 그렇다면 js 파일을 포함합니다.

또한 배열을 사용하여 다중 js 파일을 전달할 수 있습니다.

샘플 컨트롤러

class mypage extends CI_Controller{ 
    public function index(){ 
    $data['js_to_load']=array("index.js","index1.js","index2.js"); 
    $this->load->view('header',$data); 
    $this->load->view('my_html_page'); 
    } 

    public function second_page(){ 
    $data['js_to_load']=array("second.js","second2.js","second2.js"); 
    $this->load->view('header',$data); 
    $this->load->view('my_second_html_page'); 

    } 
} 

header.php -보기 당신의 HTML로 바닥 글에해야 완전히 당신의 .js를로드하는 것이 좋습니다

<!-- Needed html code here --> 

<? if (is_array($js_to_load)) : ?> 
<? foreach ($js_to_load as $row): 
    <script type="text/javascript" src="assets/js/<?=$row;?>"> 
<?endforeach;?> 
<? endif;?> 

<!-- Other html code here --> 
+0

주어진 컨트롤러 메서드에서 $ js_to_load가 정의되어 있는지 확인하기 위해 [isset();] (http://www.php.net/isset)을 사용했습니다. 그렇지 않으면 오류가 발생했습니다. ' \t

1

데이터의 헤더에 추가하려는 j의 배열을 전달하고 뷰에 추가하면 다른 방법이있을 수 있습니다 (이 종류는 컨트롤러/뷰 라인을 희미하게하므로 약간 더러 우며, 그러나 그것은 작동한다).

public function index(){ 
    $data['title'] = "Register";  
    $data['js'] = array('link', 'link', etc); 
    $this->load->view("site_header", $data); 
    $this->load->view("site_nav"); 
    $this->load->view("content_register"); 
    $this->load->view("site_footer"); 
} 
0

귀하의 문제를 이해하지 못합니다. 내가보기 site_header 또는 site_footer보기에서 js 파일을로드한다고 가정 해 봅시다. 왜이 페이지에 대한 다른보기를로드하지 않습니까? 예를 들어 :

public function index(){ 
    $data['title'] = "Register";   
    $this->load->view("site_header_extra_js", $data); 
    $this->load->view("site_nav"); 
    $this->load->view("content_register"); 
    $this->load->view("site_footer_extra_js"); 
} 

또 다른 해결책은,이에 대한 $this->uri->segment()을 사용할 수있는 경로를 확인하는 별도의 js 파일을로드 뷰에있는 경우를 넣어하는 것입니다.

+1

그것은 여러 다른 헤더 파일을 가지고 있습니다. 그러나 귀하의 제안은 논리적인데, 이는 그러한 간단한 일을하기에는 너무 미세한 관리입니다. 도움을 주셔서 감사합니다. – Binaryrespawn

0

파일 그 위에 스크립트를 실행하기 전에로드하십시오.

이유

내가를 처리하기 위해 보편적 바닥 글을 사용하기위한 here 참조하십시오.JS 파일

public function index() 
    { 
     $this->load->view('template/header'); 
     $this->load->view('template/nav'); 
     $this->load->view('thing/landing'); 
     $this->load->view('template/footer'); 
    } 

내 바닥 글보기는 사용중인 컨트롤러의 어떤 컨트롤러 또는 방법 검출 $this->router->class; 또는 $this->router->fetch_method();를 사용하여

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

$caller_class = $this->router->class; 
$caller_method = $this->router->fetch_method(); 
?> 

<script type="text/javascript" src="<?php echo base_url(); ?>public/js/jquery.min.js"></script> 

<?php 
//look for JS for the class/controller 
    $class_js = "public/js/custom-".$caller_class.".js"; 
    if(file_exists(getcwd()."/".$class_js)){ 
     ?><script type="text/javascript" src="<?php echo base_url().$class_js; ?>"></script> 
     <?php 
    } 

    //look for JS for the class/controller/method 
    $class_method_js = "public/js/custom-".$caller_class."-".$caller_method.".js"; 
    if(file_exists(getcwd()."/".$class_method_js)){ 
     ?><script type="text/javascript" src="<?php echo base_url().$class_method_js; ?>"></script> 
     <?php 
    } 
?> 
</body> 
</html> 

입니다. 그런 다음 컨트롤러/메소드에 대한 .js 파일이 있는지 확인하십시오.

작은 관리를 사용하면 바닥 글을 들여다 보지 않고 컨트롤러 및/또는 메소드 특정 스크립트를 사용할 수 있습니다.

파일 pub/js/custom-mycontrollername-mymethod.js에서 사용하면 (jquery); (JQuery와 권장으로) 문서가 ready이며 또한 DIV 페이지에 실제로 있는지 확인하기 위해 if ($("#mydivthing").length) {를 사용할 수있는 경우

$(document).ready(function() { if ($("#mydivthing").length) { $('#mydivthing').html("fetching.."); $.ajax({ ........ }); } else { console.log("skipped js segment"); } }); 

여기 JQuery와 만 실행됩니다. 그래서 당신은 그들이 (스피드를 위해)로드되지 않을 때 이벤트를 발사하지 않습니다. 하지만 jquery는 확인 작업을 수행하기 때문에 단일 버튼 클릭 이벤트보다 코드 실행이 많은 경우에만 유용합니다.

조회가 문제가되는 동안이 답변은 컨트롤러/메소드 수준에 도달합니다. 그래서 메소드는 여러 뷰를 가질 수 있습니다 - 이것은 여전히 ​​사용자가 보는 뷰/스크린입니다.

보너스를 추가하면 .js 파일을 파일 이름으로 컨트롤러/메소드에 연결할 수 있습니다.

관련 문제