It’s definitely possible. And although it might seem trivial at first, making a truly reusable “rating component” turns out to be a quite involved task.
I took a stab at it and came up with a rather decent solution that I hope you’ll like. Make sure to grab a white star.png image from material icons pack and put it in the project root first.
Here’s the code:
<App Background="#fff">
<!-- this StackPanel here is the reusable component. you should put it in a separate UX file. -->
<StackPanel ux:Class="RatingComponent" Height="40" Orientation="Horizontal">
<int ux:Property="Rating" />
<int ux:Property="Stars" />
<JavaScript>
var Observable = require("FuseJS/Observable");
var total = this.Stars;
// the .mapTwoWay() is the really tricky part that binds to the outer Observable passed in and updates it as necessary
// it might work with .innerTwoWay(), but I did not test
var rating = this.Rating.mapTwoWay(function(v) {
return v;
}, function(v, sv) {
return v;
});
var stars = [];
for (var i = 0; i < total.value; i++) {
stars.push(new Star(i));
}
function Star(id) {
this.id = id;
this.isActive = Observable(false);
}
function selectStar(args) {
rating.value = args.data.id + 1;
}
rating.onValueChanged(module, function(x) {
stars.forEach(function(s) {
if (s.id < x) {
s.isActive.value = true;
} else {
s.isActive.value = false;
}
});
});
module.exports = {
stars: stars,
selectStar: selectStar
};
</JavaScript>
<Each Items="{stars}">
<Panel HitTestMode="LocalBounds">
<Clicked>
<Callback Handler="{selectStar}" />
</Clicked>
<WhileTrue Value="{isActive}">
<Change theStar.Color="#FFC107" Duration="0.16" />
</WhileTrue>
<Image ux:Name="theStar" File="star.png" Color="#9E9E9E" />
</Panel>
</Each>
</StackPanel>
<!-- this here is the "main" app that makes use of the component -->
<JavaScript>
var Observable = require("FuseJS/Observable");
var rating = Observable(2);
rating.onValueChanged(module, function(x) {
console.log("rating changed to: " + x);
});
module.exports = {
rating: rating
};
</JavaScript>
<RatingComponent Stars="5" Rating="{rating}" Alignment="Center" />
</App>