jQuery .data and .get

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

jQuery .data and .get

Post by mrgoldy »

Hiya hello,

I was just wondering what the best method / practices are for getting variables to a jQuery script.
I understand that you can add it as a data- attribute, as I am currently doing. This works perfectly for small information required. But for example when I want to send an array of a thousand words to jQuery, I can imagine that it's far from perfect to add that as an data- attribute in an HTML element.

Then I could do it within a <script>-tag, and assign a variable there. But as I understand, this is not recommended as you do not want any 'in-line' script tags? As 3.2 is not adhering to this already (right?), I could aswell do it but is it wise?

Alternatively, I can use a jQuery .get()-request to get the information, so I do not clumb up the HTML file and follow the best practices.
But then what route should I use for the request? And is this better than the forementioned options, as might not 'clumb up' the source, but does add a request?
- Do I use a custom route from my extension and send a response from my controller?
or
- Do I simply point to a file in my extension: <root>/ext/vendor/extname/file.php and send a response from there?
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
Kuzia
Registered User
Posts: 33
Joined: Sat Dec 31, 2016 6:59 pm
Location: Moscow
Name: Alex

Re: jQuery .data and .get

Post by Kuzia »

posey wrote: Fri Feb 16, 2018 3:39 pm Then I could do it within a <script>-tag, and assign a variable there. But as I understand, this is not recommended as you do not want any 'in-line' script tags? As 3.2 is not adhering to this already (right?), I could aswell do it but is it wise?
Wise enough, if you do not transfer any user-related or system data like so. I'm doing so often for some datasets or simple vars via json.

The problem is relatively simple:
phpbb is a page-based forum engine, not an SPA. So all of the data collection intended to run on page loading. Any other ajax requests give you nothing but some loading overhead, there is only one way to overcome this - do it async, but you need to write bunch of callbacks to do so properly.

Another vay to send some small and not-often-updated data - cookie.

So the answer is simple:
If your data is not "secure" - json it and echo to <script> tag. Just be careful, your var is in window obj
If your data is small (simple string, or small array) and need to be a little safe - use cookie (but cookies still not secure enough)
If your data is super-duper secure - get it over js get request to your backend, and provide some lvl of encryption on the server (https/tls)
Post Reply

Return to “Extension Writers Discussion”