I have spent time trying to figure out this by looking through docs, examples and other threads, but so far haven’t been able to find what I am looking for. I need a way to affect an item in an object array that is called from a ux Each class. Here are several questions i have, answers to any of which I think would solve my problem:
- How can I call a function from within my UX and pass a value to it as a parameter? (I could store { item: } inside the array and pass it to a function using a callback)
- Can i use toggle control to affect a value of an object stored in the Each item being referenced? In this example below the toggle control works, and the properly executes, but as you can see from the debug that the value in the “Items” object has not changed.
- Can i access the item being used in Each directly, and use a Set, Change, or javascript to affect a value in the object being looped through?
- Normally using an Observable variable would allow you to change values of variables from the UX. How do you create an Observable array?
Here is some code I wrote that demonstrates what something like this would look like. I want to figure out how to adjust this code in order to toggle the boolean value of “happy” stored in “items” when clicked on. Clicking on the items should affect the “items” variable, which would mean that printHappyTotal()'s console output would change, and the text output of {happy} would also be affected.
<App Background="#222">
<JavaScript>
var items = [
{ name: "Jake", happy: false },
{ name: "Julie", happy: true },
{ name: "James", happy: true },
{ name: "Justin", happy: false },
{ name: "Joseph", happy: true },
{ name: "Josie", happy: true },
{ name: "Jessica", happy: true },
{ name: "Jacob", happy: false },
{ name: "Jake", happy: true },
{ name: "Jennifer", happy: false },
{ name: "Jerry", happy: true },
{ name: "Jerad", happy: false }
]
function printHappyTotal(){
var numHappy = 0;
for (var i = 0; i < items.length; i++) {
if (items[i].happy) { numHappy++ }
}
console.log(numHappy + "/" + items.length + " people are happy.")
}
module.exports = {
items: items,
printHappyTotal: printHappyTotal
};
</JavaScript>
<ScrollView>
<StackPanel>
<Each Items="{items}">
<Rectangle CornerRadius="10" Margin="5" Height="100" Color="#444">
<ToggleControl>
<Clicked>
<Toggle Target="{happy.value}"/>
<Callback Handler="{printHappyTotal}"/>
</Clicked>
<Panel ux:Name="Fade" Margin="2" Color="#000" Opacity="{happy} ? 0 : 0.2"/>
<WhileTrue>
<Change Fade.Opacity="{happy} ? 0.2 : 0" Duration="0.2" Easing="QuadraticInOut" />
</WhileTrue>
</ToggleControl>
<StackPanel Alignment="Center">
<Text FontSize="25" TextColor="#fff" Value="{name}" />
<Text FontSize="15" TextAlignment="Center" TextColor="#fff" Value="Happy: {happy}" />
</StackPanel>
</Rectangle>
</Each>
</StackPanel>
</ScrollView>
</App>
Thanks for your help!