Given the following code, I’m trying to create some TextInput fields based on a JSON object. The JSON holds the names and the captions/placeholders which should be used to render the TextInputs. The code does render as wished but I can’t create the Observables described in the JSON name -> ‘serial’ and ‘model’. Fuse reports cannot read property ‘serial’ when I try to get the value of database.dbFields.serial.value
where serial and model are the values of the JSON keys name.
In this way I may read/write the two TextInputs and create dynamically as many TextInputs as the dB fields has (as I would like to ‘bulid’ the UX interface dynamically, not with a number of fix Input fields).
function saveNewItem(arg) {
// Here goes the call to a API (PHP) wich saves 'name' and 'model' to a dB
// console.log reports: cannot read property...
console.log("DB: " + JSON.stringify(database));
console.log("fields: " + JSON.stringify(fields));
console.log(database.getAt(0).dBfields[0].name);
}
And got next message.
DB: {"_origin":-30,"_subscribers":[{"version":2,"active":true}],"_isProxy":true,"_values":[{"dBfields":[{"name":"serial","caption":"SERIAL"},{"name":"model","caption":"MODEL"}]}],"_subscriptionWatchers":[{"watcherId":14}],"_beganSubscriptions":true}
fields: {"_origin":-27,"_subscribers":[{"version":2,"active":true},{"version":2,"active":true}],"_isProxy":false,"_values":[{"dBfields":[{"name":"serial","caption":"SERIAL"},{"name":"model","caption":"MODEL"}]}],"_beganSubscriptions":true}
serial
settings is Observable. So I use Observable.getAt(position number). And dBfields isn’t Observable. Just array. So I use dBfields[0].
Thank you for taking time to help me. Now I understand.
But if my purpose is to create dynamically a number of UI TextInputs based on the JSON, how I can get the value of serial? When the user enters some data into the TextInput how I get its value?
database.getAt(0).dBfields[0].name.value
returns null.
serial should be an Observable (var serial = Observable()) and it shoud be exported to be reactive in the UX. How I can set a JSON value as it is serial to be an Observable?
And YES, the number of TextInput may increase: its number is set by the JSON which should ‘describe’ the fields that should be rendered in the UX. This is why I’m trying to set a ‘unlimited’ number of Observables based on the values of the JSON (in this case serial and model). As said, the purpose is to display ‘dynamically’ a number of TextInputs according to the fields contained a the database which is not known.
The matter now is that I’m not able to read the values of the TextInputs as
items.dBfields.getAt(0).name.value
is getting only the first one (due to getAt(0)) but if I change it to:
items.dBfields.getAt(i).name.value
i++;
it will still return only the first one and it won’t loop (i = 0 -> i = 1 -> etc.). I really don’t understand.
But a part all the above posts, as said, my purpose is to add to the UI a undetermined number of TextInputs which are described by a JSON or whatever kind of data that can be loaded by the app before the UI is drawn. These TextInputs must be reactive as they will be populated by data fetched from a dB and will be used to write data to a dB. Any idea how to create TextInputs dynamically?