Welcome Guest, Not a member yet? Register   Sign In
Execute controller from view
#21

[eluser]Gilles_T[/eluser]
Thank you for your advices. In the meantime I have used a simple way to prototype the page layout by calling views in views.

I had totally missed the true/false parameter in the load->view so that the content of a view is assigned to a variable which is passed to the global view.
Code:
$this->load->view('tbord_panneau_synthese_view',array(
                 'esp_html'  => $this->load->view('esp_panneau_tbord_view',$data_esp,true),
                 'conv_html' =>  $this->load->view('conv_panneau_tbord_view',$data_conv,true),
                 'agd_html' => $this->load->view('agd_panneau_tbord_view',$data_agd,true),
                 'fic_html'  =>  $this->load->view('fil_panneau_tbord_view',$data_fil,true),
                 )
             );

Hope this is in line with MVC best practice...

Anyway, I'm going to dig in the HMVC direction in order to set up a clean architecture and much easier to maintain.
#22

[eluser]nuwanda[/eluser]
Am I misreading this thread?

A controller is designed to pull data from a model and send it to a view. The reason a controller exists at all is to respond to a url. The url calls an action, a job to be done.

If the action is to display a dashboard of user data, then the controller pulls the user data from a model and sends it to a view where it is displayed. There's no need to call the model from the view, in fact, that defeats the controller's purpose.

The question of which user's data to display is answered by the controller. The url (which calls the controller/function) will specify the user name, id, etc. to be displayed. The controller will then pull the user data from the model and pass it to the view.

Now, granted, if you want a dynamic dashboard view, you need to use ajax. But the ajax calls are nothing more than background url calls which still need to be processed by the controller, that is, without a page refresh.
#23

[eluser]Gilles_T[/eluser]
nuwanda,

I do the refresh the panel with an javascript reload, and that is clean (I guess). But for the initial display, I directly call the function in the controller that generates the HTML. This is where the view calls the controller.

To be more specific, I have a controller dashboard.php that contains the index function and a set of couple of functions panel_x_view and panel_x_get_view (meant to display the X panels) :

-> panel_x_view generates the html for the particular information panel ($d)
Code:
return $this->load->view('panel_x_view',$d,true);

-> index function calls the main view to which the html generated above is passed :

Code:
$this->load->view('dash_main_panel_view',array(
                                  'panel_x_html'  => $this->panel_x_view($data),
                                  'panel_y_html' => $this->panel_y_view($data),
                                  'panel_z_html'  => $this->panel_z_view($data),
                                  'panel_t_html'  => $this->panel_t_view($data)
                                                                  ));

-> panel_x_get_view is just a page wrapper that gets make a page header, calls the html of panel_x_view and make a footer

-> dashboard main window will call the panel_x_get_views the following way :

Code:
<?php if($panel_x_html != null):?>
            <!-- script type="text/javascript">
                var auto_refresh = setInterval(
                               function ()
                               {
                               $('#load_panel_x').load('<?=site_url()?>/dashboard/panel_x_get_view').fadeIn("slow");
                               }, <?=TBORD_REFRESH+80?>);  // 80 is different for each panel to avoid simultaneous calls
            <!-- /script>
            <div id="load_panel_x" class="dashboard_panel">&lt;?=$panel_x_html?&gt;</div>
        &lt;?php endif;?&gt;

The problem is, if I don't call the display, I have to wait until the page refresh...

The core question is : Is it technically risky to call a controller from a view ? May it generate some bad behavior at server level or at the user's session level ?

Regards
#24

[eluser]nuwanda[/eluser]
Do you have a fixed number of panels in your dashboard view?
#25

[eluser]Gilles_T[/eluser]
No, depending on the context, some panels of the dashboard are not shown.
#26

[eluser]nikes[/eluser]
Or the View can call the Model directly.
#27

[eluser]Unknown[/eluser]
I m doing

$.ajax({
async: false,
url:'&lt;?php echo base_url('user/demo/sample_foo'); ?&gt;',
dataType: 'json',
success: function(result){
if(result == true)
alert("random text");
else
alert("some_other_random_text");
}
});

In demo controller function sample_foo returns true or false values only, but when i alert it shows "[object,object]"
What can i do to retrive true and false value ?




Theme © iAndrew 2016 - Forum software by © MyBB