We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

dynamic ajax js/css load

hello. I dont know which js files will be required for my application during interactive. So i need to download files dynamically while user executes some ajax controllers. is it possible to do this with native Phalcone classes? thx


I think Phalcon doesn't provide a native mechanism for loading "new" js/css files after an Ajax request. In my opinion this is a typical task for javascript on the clientside. You can create a Json/Xml response with Phalcon and divide the response contents into "content", "css", "js" and so on. "css"/"js" contains the urls to the resources, "content" the rendered controller contents which is loaded via Ajax. Next, you have to parse the Ajax response in Javascript, extract the different parts and load your new resources via Javascript.

If the js/css files are not too large you can use the Phalcon Asset Manager to merge the resources together and deliver them on each request. http://docs.phalcon.io/en/latest/reference/assets.html

edited Oct '14

Hi @almas59ru

It is not backend task I think the best way to complete you task will be frontend libaries like, for js

http://requirejs.org/ http://requirejs.org/docs/whyamd.html

So as I understand you is a russian guy :) read something in http://habrahabr.ru/post/152833/

With it you need to use js build systems like grunt or gulp Gulp is more priority )

Read links what I posted and compelete your task, have fun with It :)

edited Oct '14

You don't need any libraries to dynamic js/css loading via ajax also.

This is standard task of javascript and DOM.

See MDN and Stackoverflow.

Moreover, Phalcon allows for more efficient use ajax technology with backend views generation.