【发布时间】:2019-03-18 12:13:01
【问题描述】:
在过去的几个月里,我一直在学习 PHP 并使用 Octobercms,但我对它还是很陌生。我要实现的基本概念是一个包含 5 个图块的页面。当我单击磁贴时,它会根据您单击的磁贴将当前页面替换为部分页面,并更改 url 而无需重新加载页面。每个图块都有一个 id,其中包含它应该返回的部分名称。例如,设置磁贴有一个data-id="settings"。这是页面截图
我为此创建了自己的插件并将组件放置在页面上。在渲染时,它返回有效的仪表板部分,如上面的屏幕截图所示,问题是当我单击另一个图块时。它进行 ajax 调用并在我的组件中调用我的“测试”方法,并传递包含要返回的部分名称的 tile id。我使用方法$this->renderPartial('partialName') 但是我收到以下错误
Call to a member function setComponentContext() on null
以下是我其余代码的屏幕截图:
$( document ).ready(function() {
$(document).on('click','.tile',function() {
let page = $(this).attr('id');
history.pushState({page}, '', 'planner/' + page );
getPage(page);
});
window.onpopstate = function(e){
if(e.state){
getPage(e.state.id);
}
};
history.replaceState({page: null}, 'Default state', './planner');
function getPage (page) {
$.ajax({
url: '/planner/' + page,
type: 'GET',
success: function(data){
$('.page-container').html(data);
},
error: function(data) {
console.log('Could not return page');
}
});
}
});
<?php
Route::get('/planner/{page}', 'myName\budgetplanner\Components\app@test');
<?php
namespace myName\budgetplanner\Components;
use Db;
class app extends \Cms\Classes\ComponentBase
{
public function componentDetails()
{
return [
'name' => 'budgetplanner',
'description' => 'Manage your finances.'
];
}
public function onRender()
{
echo ( $this->renderPartial('@dashboard.htm') );
}
public function test($page)
{
if ($page == 'undefined') {
echo ( $this->renderPartial('@dashboard.htm') );
}
elseif ($page == 'overview') {
echo ( $this->renderPartial('@overview.htm') );
}
elseif ($page == 'month') {
echo ( $this->renderPartial('@month.htm') );
}
elseif ($page == 'reports') {
echo ( $this->renderPartial('@reports.htm') );
}
elseif ($page == 'budget') {
echo ( $this->renderPartial('@budget.htm') );
}
elseif ($page == 'settings') {
echo ( $this->renderPartial('@settings.htm') );
}
}
}
我尝试进行一些测试,并认为我已经找到了问题,但我真的不明白如何解决它。这是一些额外的屏幕截图
我转储组件对象 testing component 渲染时看起来不错 onRender 现在它是空的? clicked settings page
【问题讨论】:
标签: php oop octobercms octobercms-plugins