0

I am currently loading parts of my site using ajax (jquery 1.x in a ZF2 app), which is working nicely, until i come to load a view that has dependent javascript/css files declared in the head of that view. Declaring setTerminal(true) on the ViewModel only returns the content portion of the view, so any $this->headScript()->appendFile(), $this->inlineScript()->appendFile() and $this->headLink()->appendStylesheet() files are not included.

This means that whilst the content is correctly loaded via ajax, it is not functional if there are additional scripts that need to be used in that view.

Is there an approach of including these files somehow, so that the ajax-requested content works?

As an example: Say i am developing a dashboard, that uses ajax to pull data from several different controllers, such as User information, Subscription information etc. In the dashboard i could just include all the javascript/css files that are defined in the User and Subscription views, but surely that is just polluting the Dashboard view with a lot of js/css files? There must be a better way.

John Crest
  • 201
  • 1
  • 4
  • 24

1 Answers1

0

If you are loading parts of the page by ajax, ie directly html portions, your partial templates should not have <head> part. Even if it has one (not sure), as your main page already have a <head>wich has been managed, the partials ons won't have any effect.

So don't use $this->headScript() and $this->headLink() helpers. And even if you include your script beetwen <script> tags, your code will be inserted in DOM but it won't been interpreted. To do this, read this other question.

Community
  • 1
  • 1
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
  • Thanks for the reply. My partials do not have sections, but do contain `$this->headScript()->appendFile` etc to load view-specific js and css, which is needed for interaction on the elements created in the view. These are needed for when the page is requested without an XHR. For ajax requests, i am having better results with jquery's `$.getScript` following the success of an ajax call and injection into the DOM. Using eval is a poor choice i'd rather avoid. – John Crest Sep 12 '16 at 12:22