We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 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



6.6k

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. https://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

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

So as I understand you is a russian guy :) read something in https://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 :)



8.1k
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.