Question

I have some php and javascript (/jquery) programming experience, and just started to learn about Joomla and MVC.

MVC coded components are driven bij user actions, but so are many type javascript programs (for example those who use buttons, select menus etc). When should one use mvc patterns and when should one use javascript?

A second question, does any one have a (link to a) simple code example that demonstrates the integration of jquery code in php (view file)? (I hoped to find that in http://docs.joomla.org/Adding_JavaScript...)

Was it helpful?

Solution

From your comment is seems that you want to know when to use Javascript and when not to use Javascript.

That is a very subjective question. Personally, I think that you should not rely on javascript for functionality if you do not have a good reason to do so (for example performance concerns) because it restricts accessibility and is bad for SEO. See also these two posts.

Regarding your example: Nowadays, MVC is kind of a fussy term. But in option b, there is just one Javascript function, which handles everything. So you are not separating Model, View and Controller, so it cannot really be called MVC. And to which option is prefered: It is a matter of taste. I would prefer option a, because if you do want your website to be accessible, you would have to code a fallback for option b which seems like a lot of unnecessary work. The only downside is that you have an unnecessary extra request to the server.

And as to your second question: You do not integrate Javascript in php. Php is executed server-side (and then generally returns a generated html page to the user) while Javascript is executed client-side (and can for example change the html code that the user got).

OTHER TIPS

I'm not an MVC expert. Although from what I've read, the choice of Javascript or PHP doesn't have much bearing (by itself) on whether you're following MVC style. That said, you do still need to observe the differences between server-side (PHP) and client-side (JS/jQuery) in order to make things work consistently.

For most sites, you would work on the principle that Javascript only gets used for things that server-side code can't possibly do. I generally try to get everything functionally working using purely server-side code. And by that, I mean no user will fail at doing something essential when they don't have Javascript enabled, or if it's unavailable. I then add Javascript on top for bells and whistles. I have Javascript handle essential features as well, but only when my PHP code can already do them too as a fallback.

An example might be that you have a button that loads some new data. You can have Javascript fetch and display that data dynamically using Ajax, which can be more user-friendly. But when there's no Javascript execution available on the client, the data should load by loading a new page.

jQuery/PHP might look something like this:

$('.elem').click(function(){
    $(this).val('<? echo $phpvar; ?>')
});

Whether this is an "integration" is up for semantic debate. You can have PHP output JS code, including data to assign to its parameters as above, but that's about as far as JS and PHP can be made to play together. Really, though, PHP is run, and then Javascript is run, using whatever JS stuff you may have had PHP output. Note this can, generally, only be done on a .PHP page that has the Javascript code in <script> tags, and not on .JS pages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top