2012-02-04 46 views
3

我試圖在JQM中編寫一個Cake網站,但每次點擊一個鏈接時,屏幕都會跳轉到只包含單詞「Undefined」的頁面。CakePHP - jQuery Mobile返回包含單詞「Undefined」的空白頁

檢查源代碼顯示存在一個包含此單詞的data-role =「page」的新div,並且舊頁面的原始內容保持不變。

這是我移動視圖佈局:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title><?php echo (isset($title_for_layout)) ? $title_for_layout : 'My default title';?></title> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <?php 
      // javascript 
      $javascripts = array(
        'jquery', 
        'jquery.mobile-1.0.min.js', 
       ); 
      echo $this->Html->script($javascripts); 
      // css 
      $css = array('jquery.mobile-1.0.css','jquery.mobile.structure-1.0.css','themes/mobires.css'); 
      echo $this->Html->css($css) . "\n\t"; 
     ?> 
    </head> 
    <body> 
     <div data-role="page"> 
      <?php echo $this->Session->flash(); ?> 
      <div data-role="header"> 
       <h1> 
        <?php echo (isset($title_for_layout)) ? $title_for_layout : 'My default title';?> 
       </h1> 
       <div data-role="navbar"> 
        <ul> 
         <li><?php echo $this->Html->link('Home',array('controller'=>'pages','action'=>'display','home'))?></li> 
         <li><?php echo $this->Html->link('Contact',array('controller'=>'pages','action'=>'display','contact'))?></li> 
        </ul> 
       </div><!-- /navbar --> 
      </div><!-- /header --> 
      <div data-role="content"> 
       <?php 
        echo $this->Session->flash(); 
        echo $content_for_layout; 
       ?> 
      </div> 
      <!-- start footer --> 
       <div data-role="footer"> 
        &copy; 2012<?php if (date("Y") > 2012) { echo '-' . date("Y"); } ?> Mobires. All Rights Reserved. 
        <br /> 
        &nbsp; 
        <?php echo $this->Html->link('Privacy Policy',array('controller'=>'pages','action'=>'display','privacy'))?> 
        &nbsp; 
        <?php echo $this->Html->link('Terms & Conditions',array('controller'=>'pages','action'=>'display','terms'))?> 

       </div> 
     </div><!-- /page --> 
     <?php echo $this->Js->writebuffer(); ?> 
    </body> 
</html> 

和我home.ctp只是有單詞「測試」在裏面。我試圖單擊我的頁腳中的按鈕作爲測試Ajax加載的方式。

回答

2

如果你想將cakephp與jquery mobile整合,在我看來這是最好的解決方案。還解決了cakephp 2.x上錯過的空白頁面。你也可以看到一個如何在cakephp控制器中獲得視圖文件名(渲染文件)的例子,其中Inflector ::下劃線($ this-> action)

public function beforeFilter() { 
$this->isMobile = false; 
     if ($this->RequestHandler->isMobile()) { 

      // required for CakePHP 2.2.2 
      $viewDir = App::path('View'); 
      // returns an array 
      /* 
      * array(
      *  (int) 0 => '/var/www/maps-cakephp2/app/View/' 
      *) 
      */ 
      //path to your folder for mobile views . You must modify these lines that you want 
      //in my case I have views in folders in to /app/view/mobile and here users folder etc. 
      $mobileView = $viewDir[0] . 
        'mobile' . DS . $this->name . DS; 
      $mobileViewFile = $mobileView . 
        Inflector::underscore($this->action) . '.ctp'; 

      if (file_exists($mobileViewFile)) { 
       $this->isMobile = true; 
       $this->viewPath = 'mobile' . DS . $this->name; 

      } 

    } 
} 

public function beforeRender() { 
    if ($this->isMobile == true) { 
     $this->autorender = true; 
     //app/View/Layouts/mobile.ctp 
     $this->layout = 'mobile'; 
    } 
} 
+0

你可以也可以在cakephp中使用新的主題功能來查看視圖 – Martin 2014-04-07 14:12:28

1

啊,JQM期望完全格式化的JQM響應,所以整個「頁面」部分必須被返回,而不僅僅是內容。

+0

嗨安迪。我有同樣的問題...你怎麼得到完全格式化的CakePHP JQM響應? – hasentopf 2012-04-10 17:10:19

+0

好吧,我在這裏找到了解決方案:http://stackoverflow.com/questions/6385714/cakephp-ajax-layout – hasentopf 2012-04-10 17:31:59

+0

我只是通過聲明一個'header'來添加從content_for_layout中缺少的JQM響應的位我在內容前後輸出的「頁腳」元素。如何關閉請求處理程序爲您工作? – Andy 2012-04-11 05:47:51