Well, I feel kind of embarrassed that I need to write about this topic... I always thought I kind of understood MVC architecture. But this one drove me nuts:
I am working on a custom article management system for my front end users. I know that there might be (for sure) extensions to accomplish this, but you know how it is... you want to do it yourself. Everything worked out fine, until I got to the point of sorting the article table (alphabetically, chronologically etc.). This, too, could be done with extensions or existing jquery / other techniques. Could be.
Well, I decided to do it with the traditional Javascript / Ajax / PHP approach. Jacvascript function triggers AJAX request, AJAX request goes to controller, controller queries the database and creates an array that can be handled by the view.
So far so good. But how do I get the view to reload without refreshing the entire page (which would obviously loose the sort order)? I made it as far as updating the model, but I couldn't manage an update of the view - yes, it reloaded, but the array with the data for the table seemed to be empty, even though I set it. I was about to do copy and paste... but I am very reluctant to redundancy.
Eventually, my controller calls a custom view method and passes the array (without using the model), then the controller calls a custom display method. And voilà , it works.
The code:
controller.php
...
$db->setQuery($query);
$result = $db->loadObjectList();
$view = $this->getView(<viewname>, 'html');
$view->setMyArticles($result);
$view->mydisplay();
If your view class name is "MyviewViewSpecialname", then viewname would be "specialname" (lowercase). This is important if you have multiple views.
view.html.php
// Overwriting JView display method
function display($tpl = null)
{
// Assign data to the view
$this->msg = $this->get('Msg');
$this->myarticles = $this->get('MyArticles');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}
// Display the view
parent::display($tpl);
}
public function mydisplay($tpl = null){
parent::display($tpl);
}
public function setMyArticles($array){
$this->myarticles = $array;
}
This is it! This cost me about 20 hours to figure it out... Hope it can help you, too!
I am working on a custom article management system for my front end users. I know that there might be (for sure) extensions to accomplish this, but you know how it is... you want to do it yourself. Everything worked out fine, until I got to the point of sorting the article table (alphabetically, chronologically etc.). This, too, could be done with extensions or existing jquery / other techniques. Could be.
Well, I decided to do it with the traditional Javascript / Ajax / PHP approach. Jacvascript function triggers AJAX request, AJAX request goes to controller, controller queries the database and creates an array that can be handled by the view.
So far so good. But how do I get the view to reload without refreshing the entire page (which would obviously loose the sort order)? I made it as far as updating the model, but I couldn't manage an update of the view - yes, it reloaded, but the array with the data for the table seemed to be empty, even though I set it. I was about to do copy and paste... but I am very reluctant to redundancy.
Eventually, my controller calls a custom view method and passes the array (without using the model), then the controller calls a custom display method. And voilà , it works.
The code:
controller.php
...
$db->setQuery($query);
$result = $db->loadObjectList();
$view = $this->getView(<viewname>, 'html');
$view->setMyArticles($result);
$view->mydisplay();
If your view class name is "MyviewViewSpecialname", then viewname would be "specialname" (lowercase). This is important if you have multiple views.
view.html.php
// Overwriting JView display method
function display($tpl = null)
{
// Assign data to the view
$this->msg = $this->get('Msg');
$this->myarticles = $this->get('MyArticles');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}
// Display the view
parent::display($tpl);
}
public function mydisplay($tpl = null){
parent::display($tpl);
}
public function setMyArticles($array){
$this->myarticles = $array;
}
This is it! This cost me about 20 hours to figure it out... Hope it can help you, too!
Comments
Post a Comment