Best MVC practice: Where to put this code?

I’ve got a Yii module which uses a JS component which needs to be populated by data from a parent model. I want to injected this data directly into the JS code when the module renders its view (might not be the cleanest way but works for me). For this I want to create a function that reads data from a particular model and returns a piece of JS code containing the data (it’s not much). Let’s call the function “data-to-JS”. This function will be called from said view script (yes, i’m putting JS code directly into the view script). So far no problem!

My problem is that I want to keep fairly good MVC practice but I can’t decide where to put my “data-to-JS” function.

  1. Should I place it in the model belonging to the data in question? Feels out of context to put such a "specialized" function there though.

  2. How about the module controller? Well, contextwize it kind of fits, but it’s not really a controller duty, is it?

  3. The module Class perhaps? But then again, shouldn’t a module class be kept as general and “non tainted” as possible?

  4. I could always (higher powers forbid!) do it in the view script.

Well… there’s the bone I’m currently chewing on. Any input would be highly appreciated =)

Kind regards


It sounds as if this should be written as a widget so that it can be reused in any view script of an application.

As for passing data into a JS scripts, this sounds a little fuzzy, but I guess encoding the data to a JSON string will make it usable by a JavaScript script.

I don’t know, if it’s best practices, but i wold go/think the following way:

The model class only includes functions witch related to get, manipulate or save date. For example if you need the data in an specific form/order/etc or pre-precessed (after loading) to prepare the datat, regarding to model specific things in order to have a general/universal Controller/action for different models.

The views are only used to merge variables with HTML code (if it is possible). I try to avoid application logic at views.

So it looks like, the controller is an good place for your data-to-JS function. I think, I would generate the JS inside the controller, followed by an registerScript.

If you need that function in different controllers, you can create an additional class as an component (protected/components) and place the function inside this class/component.

I’d create a widget that return the js code.

Js code is output like all other, so it shuold stay in a view. If you have to reuse the view, create a widget.

what ab out a toJSON behavior in your model?

Thanks for your input guys, I really appreciate it! Lots of good tips here.

I like the idea of creating a “helper” component class, thus keeping slightly “alien” functions aside. As for creating a widget or send data via JSON or XML would normally make sense too. However, I just need a few lines of data in this case. The module only uses two distinct views so a widget might be overkill for this application. I’m also concerned about speed. I lean towards Zaccaria’s recommendation above.