0

我正在构建一个动态视图 ( Page),它由多个名为 via 的元素(小部件)组成$this->element('messages_unread')。其中一些元素需要与页面模型无关的数据。
在现实生活中的话:我的用户将能够通过从众多元素中进行选择来构建他们自己的页面(“前 5 个帖子”、“10 条未读消息”等......)

我通过从元素内部调用来获取数据,$this->requestAction(array('controller'=>'events','action'=>'archive')每个元素的 url-variables 不同。

我知道这requestAction()是昂贵的事实,我计划通过适当的缓存来限制成本。

实际问题:
我的问题是分页。当我在Page视图中并在页面视图中调用requestAction('/events/archive')PaginatorHelper 时,将不知道Event模型及其分页器变量$this->Paginator->next()等......将不起作用。
如何实现正确的分页?我试图通过调用来设置模型,$this->Paginator->options(array('model'=>'Event'))但这不起作用。
我是否可能需要在中返回自定义定义的分页变量,requestAction从而构建我自己的?

还是有另一种甚至可以避免的方法requestAction()?请记住,请求的数据与页面无关。

亲切的问候,巴特

[编辑] 我的临时解决方案,但仍对评论/解决方案开放:
在 requestedActionEvent/archive中,返回分页器变量以及如下数据: return array('data'=>$this->paginate(), 'paging' => $this->params['paging']);

4

1 回答 1

1

I've tinkered a bit more and the following works for me, and the PaginationHelper works:

In the element:

// requestAction returns an array('data'=>... , 'paging'=>...)
$data = $this->requestAction(array('controller'=>'events','action'=>'archive'));  

// if the 'paging' variable is populated, merge it with the already present paging variable in $this->params. This will make sure the PaginatorHelper works
if(!isset($this->params['paging'])) $this->params['paging'] = array();
$this->params['paging'] = array_merge( $this->params['paging'] , $data['paging'] );

foreach($data['events'] as $event) {
    // loop through data...
}

In the Controller:

public function archive() {
    $this->paginate = array(
        'limit'     => 10
    );

    if ($this->params['requested'])
        return array('events'=>$this->paginate('Event'), 'paging' => $this->params['paging']);

    $this->set('events', $this->paginate('Event') );
}
于 2013-09-11T18:39:42.067 回答