I have difficulties with Observable arrays when trying to get to the variables inside them in JS. In UX I know that the variables of Human = Observable({firstname: "Charlie", lastname: "Kelly"})
is referenced by Value="{Human.firstname}"
. But this does not seem to apply to the JS practice as it returns Undefined (console.log("First name: " + Human.firstname)
). I’ve tried using Human.toArray()
which doen’t seem to do anything other than just make an exact copy. I’ve also tried to access the values with Human._values.firstname
wihtout any luck. There’s no specific mention of this in neither the quick documentation about Observables or the Observable API.
What is the proper practice for accessing these values in an Observable array in JS?
Please help.
Thanks in advance
In the example you mention there’s no list as the observable holds just 1 value. In that case you’d just access it as console.log("First name: " + Human.value.firstname)
This is documented at the top of the Observable API docs
Another important thing to keep in mind is that observables are lists and therefore can’t be accessed as arrays.
To manipulate elements in these lists you can use the observable list api
Thank you for the reply!
I have used JSON arrays in JS by doing Thing.something so I just assumed that Observable.value would return Undefined as Human does not have an element called “value”. But thanks for the pointers!
Hmmm still doesn’t seem to assign properly…
if i try console.log(Happening.value.title);
it prints undefined
.
Impossible to comment on that based on just that line.
If you want help then please provide more of the relevant code so we can see how Happening
is set up.
Oh sorry, I tried to avoid it since it’s a bit complicated but hang in there!
Happening = Observable();
...
this.Parameter.onValueChanged(module, function(param) {
IsHost.value = param.isHost;
console.log("IsHost.value: " + IsHost.value);
UserContext.getStorageUser().then(function(user_result) {
HappeningContext.getHappenings(param.eventId, user_result.id).then(function(happenings_result) {
if (happenings_result !== false) {
Happenings.replaceAll(happenings_result);
NoHappenings.value = false;
}
Loading.value = false;
});
});
});
...
function save() {
console.log("Happening.value.title: " + Happening.value.title);
var happeningObject = {
id: Happening.value.id,
event_id: Happening.value.event_id,
title: Happening.value.title,
text: Happening.value.text,
from: Happening.value.from,
to: Happening.value.to
};
happeningsRouter.goto("savingHappening");
if (FormMode.value == "add") {
// HappeningContext.add(happeningObject).then(function(result){
// console.log("result: " + result);
// });
} else if (FormMode.value == "edit") {
HappeningContext.set(happeningObject).then(function(result){
console.log("result: " + result);
});
}
}
Happening anatomy
I can’t see anything in that code which actually assigns any value to Happening
(just to Happenings)