Using MVC pattern (which is a MUST for most of the frameworks) you can write Javascript in a View but this approach has many disadvantages such as problematic code obfuscation or quick assembling of entire project Javasctipt while transferring it to a separate server – all these will cause difficulties.
Today I would like to share the way we organize JS files for the project in our team.
Interesting. But how do you reuse js code if you have a single js file for every action (or i did not understand maybe)? And could you please show how would you pass php data to js?
Also a good way of handling the URL for ajax requests in the javascript controller files is to add a function to our $.app object that replicates the functionality of the Yii function:
$.app = {
page: undefined,
init: function () {
if ($.app.page !== undefined) {
if (!$.app.page.delayed) // Flag, in case you don't need to load file on start
$.app.page.init.call($.app.page);
}
},
createControllerUrl: function( action ) {
return this.baseUrl + '/index.php?r=' + action;
}
};