CakePHP - jQuery Mobile返回包含单词“Undefined”的空白页面

时间:2012-02-04 14:26:56

标签: jquery cakephp mobile

我正在尝试在JQM中编写一个Cake网站,但每当我点击某个链接时,屏幕就会转换为只包含“未定义”一词的页面。

检查来源会显示有一个新的div,其中data-role =“page”包含此单词,并且原始页面的原始内容保持不变。

这是我的移动视图布局:

<!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里面只有“test”这个词。我试图点击我的页脚中的按钮作为测试Ajax加载的方法。

2 个答案:

答案 0 :(得分:2)

如果你想将cakephp与jquery mobile集成,我认为这是最好的解决方案。还解决了cakephp 2.x上错过的空白页面。您还可以看到如何使用 Inflector ::下划线($ this-&gt; action)获取cakephp控制器中视图文件(渲染文件)的名称的示例。

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';
    }
}

答案 1 :(得分:1)

啊,JQM期望完全格式化JQM响应,因此必须返回整个“页面”部门,而不仅仅是内容。