How do I insert associative arrays in plugin templates?
-
Hi, I have a simple problem. In my plugin code I am trying to insert this variable into the javascript in one of my templates:
var ticketTypes = { adult: 10.00, student: 7.50, child: 5.00 }
My best approach so far is to pass it to the template like this in a res.render call:
{"tickettypes": JSON.stringify(ticketTypes)}
(Without JSON.stringify, it doesn't show up in the results at all)
It makes it into my page in a javascript section like this:
var tickettypes = $.parseJSON('{tickettypes}'.replace(/{/g, "{").replace(/}/g, "}"));
The entity replacement is necessary because the value makes it into the template as:
'{"adult":10,"student":7.5,"child":5}'
So it "works" but feels hacky. Is there a better way to get an associative array in the client side javascript?
Thanks!
-
ajaxify.data.get('foo')
ought to do it -
@psychobunny said:
ajaxify.data.get
Woah! That's kick ass! There is a lot of data ready to go in that ajaxify.data object. That changes the way I think about loading data into NodeBB templates.
For anyone else interested, in your browser js console just type "ajaxify.data" to see what NodeBB already has loaded for you.
Is this the best spot to learn more about ajaxify? http://4nf.org/
-
Ajaxify is something we developed in-house (along with templates.js, translator.js, amongst others). The link you posted is unrelated, unfortunately.
That said, we could use with better documentation for our major components (damn! That's really lacking a lot of information.)
If you ever have free time please contribute (the edit button is on the top right).
In any case, I'm glad you figured it out
-
BTW I meant to say
ajaxify.variables.get('foo')
but looks like you've figured it out -
Well, it looks like that way does not work for me:
ajaxify.variables.get('tickettypes')
but there is lots of good stuff in:
ajaxify.data //and this works: ajaxify.data.tickettypes
Also, I was calling some of these variables in jQuery's document load and I needed to switch to this event that I found in the ajaxify.js source (which appears only to fire after jQuery's document load):
$(window).on('action:ajaxify.contentLoaded', function(event, eventInfo){ console.log("Is ajaxify available yet?" + ajaxify.data.tickettypes); }
Should I not be using this style to access values?
ajaxify.data.tickettypes
-
I don't feel like I understand ajaxify well enough to start writing docs just yet, but I take notes and maybe down the road I can write something up.
You guys seem pretty eager to roll your own stuff. So far it seems like that has worked out very well. I'd be interested in hearing some discussion about the motivations behind doing your own templates.js, translator.js, ajaxify.js, etc...
I've worked on lots of projects in the past that were just huge piles of every existing popular library and that approach certainly can have its own problems. Finding things in your source code so far has been pretty smooth and so I ask the question about motivation just out of curiosity not criticism.
-
I know nothing about this ajaxify yet, but based on what I am reading you should try
ajaxify.variables.get('tickettypes')
Instead of
ajaxify.data.get('tickettypes')
-
@jongarrison We'd love to discuss it, maybe in a new topic? I'm liking this trend tonight of meta topics
-
@HolyPhoenix Thanks for catching that typo (I edited my comment, just now). I had correctly tried @psychobunny 's approach without luck but then typed it incorrectly.
I found this post from @baris (https://community.nodebb.org/topic/5942/ajaxify-variables-get-deprecated) that says the .get('name') approach has been deprecated and that this style is current:
ajaxify.data.tickettypes
-
This post is deleted!