Reading json data in Luci JS

Hi
I am trying to rewrite my package to the new Luci JS but I stopped in one place. I have a script that outputs json data.

# /usr/share/temp/data.sh
{
"mtemp":"38 °C",
}

But I can't download this data and show it in the form.

'use strict';
'require view';
'require fs';
'require ui';

return view.extend({
		render: function(info) {

		return E([], [
			E('h2', {}, [ _('Test app') ]),
			E('h4', {}, [ _('Generat Information') ]),
			E('table', { 'class': 'table' }, [
				E('tr', { 'class': 'tr' }, [
					E('div', { 'class': 'td left', 'width': '33%' }, [ _('Chip Temperature: ')]),
					E('div', { 'class': 'td left' }, [ 'json.mtemp' ]),
					]),
				E('tr', { 'class': 'tr' }, [
					E('div', { 'class': 'td left', 'width': '33%' }, [ _('')]),
					E('div', { 'class': 'td left' }, ['']),
					]),

			])

		]);
	},

	handleSaveApply: null,
	handleSave: null,
	handleReset: null
});

Could someone write a function that will get this data and put it in the form?

Use fs.exec_direct() and ship an ACL rule allowing to execute the script:

{
  …
  "read": {
    "cgi-io": [ "exec" ],
    "file": {
      "/usr/share/temp/data.sh": [ "exec" ]
    }
  }
}
1 Like

Thank you @jow
It's always something, but if I don't see the code, I won't understand it, the wiki itself is not enough for me. Well, maybe I'll think of something :slight_smile: .

You can find a number of examples in the repo:

How you need to structure your code and where to do the call depends on what you want to achieve exactly, e.g. fetch the data on page load to render it into a table, poll it periodically, read it before form rendering to set as default value for an input, …

So far I have downloaded the data using:

XHR.poll(0, '<%=build_url("admin/data/devicedata")%>', null,
		function(x, data)
		{
                if (e = document.getElementById('temperature'))
			 	e.innerHTML = String.format(data.mtemp);
                }
        );

so I would like the data to refresh periodically. I've been browsing github for a hint anyway, but haven't found anything like it.

The old package is working so I'm in no rush. Maybe I will come to a solution somehow :smile: .

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.