LZDigital Posted January 30, 2016 Posted January 30, 2016 I know this sounds weird, but I want to be able to use the parameters passed to a template in JS, how do I go about doing this?
Colonel_mortis Posted January 30, 2016 Posted January 30, 2016 I would suggest either add the parameters in a data- attribute on one of the elements in the template, or, if absolutely necessary (it probably isn't), add a <script> tag and define the parameters as variables in there.
LZDigital Posted January 31, 2016 Author Posted January 31, 2016 yeah I tried accessing through data attributes but i cannot for the life of me get that to work. Always comes back undefined.
LZDigital Posted January 31, 2016 Author Posted January 31, 2016 $('div.example').data('twurl'); I'm trying to use this code to retrieve the data from: <div id='example' data-twurl='mydata'> </div> I'm trying to do this with a widget, but it's not working, any ideas?
Colonel_mortis Posted January 31, 2016 Posted January 31, 2016 8 hours ago, ReZnoR said: $('div.example').data('twurl'); I'm trying to use this code to retrieve the data from: <div id='example' data-twurl='mydata'> </div> I'm trying to do this with a widget, but it's not working, any ideas? Your selector is wrong - it should be $('#example').data('twurl');
LZDigital Posted January 31, 2016 Author Posted January 31, 2016 15 hours ago, Colonel_mortis said: Your selector is wrong - it should be $('#example').data('twurl'); Tried that too, still didn't work.
Marcher Technologies Posted January 31, 2016 Posted January 31, 2016 22 hours ago, ReZnoR said: I know this sounds weird, but I want to be able to use the parameters passed to a template in JS, how do I go about doing this? Set(specifically modified to support use in a template, remove the {{}} if working in a PHP file): {{\IPS\Output::i()->jsVars['my_data_key'] = $data;}} Read in JS: var myData = ips.getSetting('my_data_key'); Using the data attributes properly tends to be a bit more complex, as you end up diving into making controllers and/or models in the js framework.
LZDigital Posted January 31, 2016 Author Posted January 31, 2016 ah ok thank you very much. Only speedbump I ran into.
LZDigital Posted January 31, 2016 Author Posted January 31, 2016 1 hour ago, Marcher Technologies said: Set(specifically modified to support use in a template, remove the {{}} if working in a PHP file): {{\IPS\Output::i()->jsVars['my_data_key'] = $data;}} Read in JS: var myData = ips.getSetting('my_data_key'); Using the data attributes properly tends to be a bit more complex, as you end up diving into making controllers and/or models in the js framework. I don't know what I'm doing wrong, but I am doing exactly as you did here and I still cannot read the variable in the JS says undefined. The template variable only contains a URL so I don't know how else to get it there.
LZDigital Posted February 1, 2016 Author Posted February 1, 2016 I checked the jsVars array, and it only contains a couple of items, including my custom one. But when I print out the object in JS to check it, it contains the info of the main jsVars object with baseURL and all of those but not my custom one. How does my js file access only stuff for that plugin? Like my jslang file, my javascript file can't access it.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.