2

I have a model pulling data from two external API's, concatenating the returned arrays, and a view template that displays it. I have a sub-view that will need only one of the API's.

So my question is this:
What's actually initiating the model request to the API?
The view.html.php, or the view templates?

If it's the view.html.php file, then there's no need for a second model method and I can just reuse the request.
If the view template is pulling it, then cutting the API requests in half would make the request faster.

models/domains.php

public function getPrices() {
        $dispatcher = JEventDispatcher::getInstance();
        $prices = $dispatcher->trigger('getData', 'products/customer-price.json');
        $cats = $dispatcher->trigger('getData', 'products/category-keys-mapping.json');

        $result = array();
        $result[]['product'] = $cats[0];
        $result[]['prices'] = $prices[0];

        if (isset($cats[0]['status'])) {
            JError::raiseError(400, $cats[0]['ERROR']);
            return false;
        } else {
            if (!isset($this->prices)) {
                $this->prices = $result;
            }
            return $this->prices;
        }
    }

    public function getTlds() {
        $dispatcher = JEventDispatcher::getInstance();
        $cats = $dispatcher->trigger('getData', 'products/category-keys-mapping.json');
        $dispatcher->trigger('end');

        $result = array();
        $result[]['product'] = $cats[0];

        if (isset($cats[0]['status'])) {
            JError::raiseError(400, $cats[0]['ERROR']);
            return false;
        } else {
            if (!isset($this->tlds)) {
                $this->tlds= $result;
            }
            return $this->tlds;
        }
    }

views/domains/view.html.php

function display($tpl = null) {
    // Assign data to the view
    $this->prices = $this->get('prices');
    $this->tlds = $this->get('tlds');

    // Check for errors.
    if (count($errors = $this->get('Errors'))) {
        JLog::add(implode('<br />', $errors), JLog::WARNING, 'jerror');
        return false;
    }

    // Display the view
    parent::display($tpl);
}
4

You can actually do it either way depending on which method you prefer. The way you are currently calling the functions in the view.html.php file is fairly common but you can also call those functions from within your layout (sub-view?) by including the model on that page.

Layout 1

$model = JModelLegacy::getInstance('ViewName','ComponentNameModel');
$prices = $model->getPrices();

Layout 2

$model = JModelLegacy::getInstance('ViewName','ComponentNameModel');
$ids = $model->getIds();

Just make sure that ViewName is the name of your current view and ComponentNameModel is the name of your component followed by Model

Example: com_users component

$model = JModelLegacy::getInstance('Login','UsersModel');

(Login is the name of the view and Users is the name of the component)

$prices = $model->getPrices();

For a bit more clarification the model name can be whatever model you want to include in the template file, it doesn't have to be the name of the current view's model. If you wanted to include a "Common" model as well as your normal view model you can include both. I use this method a lot with my component development. I keep the common tasks (text formatting etc.) in a common model and include that with my view model.

  • Hi, thanks for the response, but I would rather keep my model method calls in the view.html.php. That being said, can you answer the question? Thanks! – James Shaver Feb 3 '16 at 2:39
  • 2
    If you are calling the model from inside view.html.php then that is the file that is initiating the request when your component loads. The subview is only a layout displays your data that was requested by your view.html.php file. Imagine it like this: Your view.html.php is a bucket, your model if the ocean of water. The bucket retrieves water from the ocean. The subviews are glasses that can manipulate the water in the bucket. – Terry Carter Feb 3 '16 at 3:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.