0

我正在尝试结合使用 CakePhp 和 JQuery Mobile。通常它工作得很好,但是我在使用从一个控制器重定向到另一个控制器时遇到了一个巨大的问题。

特别是因为我添加了 RequestHandler。

我认为在这种情况下的问题是,Jquery Mobile 需要一个完整的页面字符串,但控制器只是返回视图。

有什么方法可以使重定向功能与jquery mobile一起工作?

在这种情况下,我喜欢从 Orderheads 重定向到 Orderpositions

控制器订单头

    if ($this->request->is ( 'post' )) {
        $this->Orderhead->create ();
        if ($this->Orderhead->saveAll ( $this->request->data,array (
                        'deep' => true 
                ))) {
            $orderId = $this->Orderhead->findByOrdernumber( $this->request->data['Orderhead']['ordernumber']);

            $id =$orderId['Orderhead']['id'];
            $this->Session->setFlash ( __ ( 'The orderhead has been saved.' ) );


            return $this->redirect ( array (
                    'controller' => 'orderpositions', 
                    'action' => 'add', $id
            ) );
        } else {
            $this->Session->setFlash ( __ ( 'The orderhead could not be saved. Please, try again.' ) );
        }
    }

控制器订单位置

public $components = array (
        'Paginator',
        'Session',
        'RequestHandler' 
);

public function add($id = null) {
    if ($this->request->is ( 'ajax' )) {
        if ($this->RequestHandler->isAjax ()) {
            Configure::write ( 'debug', 0 );
        }
        if (! empty ( $this->data )) {
            $this->autoRender = false;
            $this->Orderposition->create ();
            if ($this->Orderposition->save ( $this->request->data )) {
                echo 'Record has been added';
            } else {
                echo 'Error while adding record';
            }
        }
    } else {
        $this->loadModel ( 'Orderhead' );
        if ($this->Orderhead->exists ( $id )) {
            $orderInformation = $this->Orderhead->findById ( $id );
        } else {
            throw new NotFoundException ( __ ( 'Invalid order id does not exists' ) );
        }
        $this->set ( compact ( 'orderInformation' ) );
    }
}
4

1 回答 1

1

好的,我自己解决了这个问题。

我的想法是,问题出在 JQuery 移动网站上。JQuery Mobile 通常使用 Ajax 进行链接。

这是或更好地说是问题,结合 cakephp flowconzept,它无法工作。因为 Jquery 总是期待整个页面(信息)。这正是为什么我的所有带有选项 rel='external' 的链接都能完美运行的原因。

顺便提一句。这就是为什么您需要 jQuery-Mobile-Subpage-Widget 来使用多页的原因。

但回到主题,为了解决我的控制器问题->重定向功能,我只需将 Data-Ajax='false' 选项添加到 cakephp 表单创建功能的选项参数中。

如果您设置该参数,则链接将设置一个完整页面请求而不是ajax 请求。

例子:

<?php
    echo $this->Form->create('Contactperson', array(
                            'data-ajax' => 'false'));

    echo $this->Form->input('name');
    echo $this->Form->input('surname');
    echo $this->Form->input('email');

    echo $this->Form->end(__('Submit'));
?>

我希望这可以帮助任何其他有同样问题的人,我在这些东西上浪费了很多时间。

于 2014-12-15T20:49:33.073 回答