For the past couple of months I've been learning PHP and using Octobercms and I'm still fairly new to it. The basic concept of what I'm trying to achieve is a page which contains 5 tiles. When I click the tile it replaces the current page with a partial depending on which tile you clicked and changes the url without having to reload the page. each tile has an id with the name of the partial it should return. e.g the settings tile has an data-id="settings"
. Here is a screenshot of the page
I've created my own plugin for this and placed the component on the page. On render it returns the dashboard partial which works and is shown in the screenshot above, the problem is when I click another tile. It makes the ajax call and calls my "test" method in my component and passes the tile id which contains the name of the partial to return. I use the method $this->renderPartial('partialName')
however I get the following error
Call to a member function setComponentContext() on null
Here are screenshots to the rest of my code:
$( 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') );
}
}
}
I tried doing some testing and think I've found the issue but I don't really understand how to go about fixing it. here are some additional screenshots
I dump the component object testing component On render it looks fine onRender now its empty ? clicked settings page