samedi 27 décembre 2014

mojolicious assign an array to a javascript variable


I have an array reference ($sections) in the stash. I'm trying to dereference this and assign it to a javascript array but nothing that i try works.


for example



%= javascript begin
var list = <%== $sections %>;
etc...
% end


leads to list equaling ARRAY0x23456 (which is correct since $sections is a ref)



%= javascript begin
var list = <%== @$sections %>;
etc...
% end


Assigns the array length to list (which is correct since list is treated as a scalar assignment).



%= javascript begin
var list = <%== $sections %>.slice();
etc...
% end


gives an error because $sections is a ref



%= javascript begin
var list = <%== @$sections %>.slice();
etc...
% end


Arguably is the most likely candidate but resolves to [array.length].slice() (e.g. 3.slice()), and fails.


How can I do this?


EDIT; I've managed to do this by forming a string with the array values in it within the stash and then creating the array I want from this.


e.g. stash contains the string 5,6,7,8 in $string within the javascript i can then do



array = [ <%= $string %> ];


I'd still be interested to know if there is a way to do this using the arrayref in the stash.





Aucun commentaire:

Enregistrer un commentaire