6

I refer to the code below, presented in Developing a MVC Component - Front End Function Example:

class HelloWorldViewUpdHelloWorld extends JView
{
    // Overwriting JView display method
    function display($tpl = null) 
    {
            $app            = JFactory::getApplication();
            $params         = $app->getParams();
            $dispatcher = JDispatcher::getInstance();

            // Get some data from the models
            $state          = $this->get('State');
            $item           = $this->get('Item');  //WHAT IS THE PURPOSE OF THIS STATEMENT?
            $this->form     = $this->get('Form');

            // Check for errors.
            if (count($errors = $this->get('Errors'))) 
            {
                    JError::raiseError(500, implode('<br />', $errors));
                    return false;
            }
            // Display the view
            parent::display($tpl);
    }

}

I don't understand the purpose of this statement and I also notice that $item does not appear in the code in file site/views/updhelloworld/tmpl/default.php on that webpage.

I actually have the same question for the view.html.php file of com_users/views/registration of Joomla 3 code ( I thought by comparing the two form submit examples I could answer my question):

    $this->data     = $this->get('Data'); //WHAT IS THE PURPOSE OF THIS STATEMENT? WHAT PRECISELY KIND OF DATA iS MEANT HERE?
    $this->form     = $this->get('Form');
    $this->state    = $this->get('State');
    $this->params   = $this->state->get('params');

And also here $this->data does not appear in the related tmpl/default.php file (?)

1
  • The really simple answer is that you need the old values when you update unless you always want to wipe them out when the user goes to edit. JForm takes care of a lot of the work of managing rendering the old values for you so you won't see direct references.
    – Elin
    May 18, 2014 at 23:34

2 Answers 2

5

In classes extending JView or JViewLegacy, a call to $this->get() will call a function in registered model classes starting with get. So calling $this->get('Item') will call a model's getItem method.

2
  • Is this still the advised best practice? I remember somewhere, in some thread that JView::get was going to be/ might be depreciated in the undetermined future. May 26, 2014 at 8:13
  • In the JView(Legacy) API, this is the recommended practice.
    – Michael
    May 27, 2014 at 13:17
3

The $this->get('Item') calls the function getItem() in the model, and subsequently it loads the data from the database.

$this->get('Data') calls a method in the model that gets the registration form data and prepares it for use.

Have a look at getData in com_users/models/registration.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.