jquery - CakePHP - jQuery Mobile 返回包含单词 "Undefined"的空页面

标签 jquery cakephp mobile

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

检查源代码发现有一个带有 data-role="page"的新 div 包含这个单词,并且我的旧页面的原始内容保持不变。

这是我的移动 View 布局:

<!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 加载。

最佳答案

如果你想将cakephp与jquery mobile集成,我认为这是最好的解决方案。还解决了 cakephp 2.x 上遗漏的空页。您还可以查看如何使用 Inflector::underscore($this->action) 在 cakephp Controller 中获取 View 文件(渲染文件)名称的示例。

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

关于jquery - CakePHP - jQuery Mobile 返回包含单词 "Undefined"的空页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9141548/

相关文章:

javascript - 是否有适用于移动设备的 JQuery 的漂亮 javascript 对话框?

jquery - 在基于百分比的设计中垂直居中文本?

cakephp - 我如何使用find语句中的 'list'连接first_name和last_name两个不同的数据库字段来分配给cakephp中的下拉列表

html - 移动浏览器未检测到移动 CSS 文件

php - Cakephp:CakeDC 用户插件并保存另一个表

sql - CakePHP 多个未找到的

c# - 数据密集型应用程序的电话差距与单点触摸

javascript - 使用 JQuery 在 textarea 中获取光标位置和突出显示的文本

javascript - 如何将 jQuery 代码转换为插件?和插件开发模式

javascript - 删除行之间的空间