Welcome Guest, Not a member yet? Register   Sign In
Javascript rendered in view fragments?
#1

[eluser]marlar[/eluser]
Hi,

In another thread I asked for recommendations on javascript libraries for rich user interfaces.

While I have not yet decided which one to go for, I have thought of a way to implement the scripts such that I can later change the library with little or no changes to the main views.

Let's say I need a popup menu somewhere which shows a list of choices for the user. Instead of hardcoding the javascript/dhtml into the view, I could instead inject the code either via a PHP variable initialized in the controller, or directly from a sub view (for example using the View Library or similar).

Then the controller or the sub view is responsible for generating the code for the popup menu. If I later change the library, I only need to modify the code centrally.

As a complete beginner with CI I might miss some important points, but does such a scheme seem wise?

Also, should it perhaps be a job for the model to deliver the html/javascript for the widgets? I know it is recommended that the models don't render the html, but this might be a special case.

Finally, can a model call another model? If so, the first model could deliver the raw data (which might be needed for other purposes) and the second model could render the code.

Regards,
Martin
#2

[eluser]ejangi[/eluser]
I personally don't think a model is that answer for delivering up JavaScript etc as models are "supposed" to be data containers unless you're talking about delivering up data as JSON or something??? I would say it is more likely that a library or helper should deliver the JavaScript/HTML, but I am intrigued with your idea about JavaScript in a view.

Models can call models:
Code:
class Blogmodel extends Model {

    var $title   = '';
    var $content = '';
    var $date    = '';
    var $CI = null;

    function Blogmodel()
    {
        // Call the Model constructor
        parent::Model();
        // Here's the punch line:
        $this->CI =& get_instance();
        $this->CI->load->model('myothermodel');
    }
}
#3

[eluser]marlar[/eluser]
I could certainly be a library or helper too. I think the actual implementation is not so important; it's more the idea that the code is put somewhere centrally so whenever I call "popup menu" I will get the right code based on whatever JS library I am going for.

When I think of it, a library (being a class) could actually be the most logical way. There could then be methods for initializing the menu, invoking it, changing the look and feel etc. By calling the menu object instead of the underlying JS library, I get exactly the desired effect: independence on the chosen library.




Theme © iAndrew 2016 - Forum software by © MyBB