Modelling a Maximum Value

So… been a while since I last used Enyo, and I see there are a ton of great improvements for me to get used to!

Anyway, I'm trying to model something with a value and a maxValue that it is clamped to, with me performing the clamping in the valueChanged() event. I'm currently doing the following (forgive typos, I'm simplifying here):
var kind = require('enyo/kind'),
Model = require('enyo/Model');

var MyModel = kind({
name: 'MyModel',
kind: Model,
attributes: { value: 0, maxValue: 100 },

maxValueChanged: function() { this.valueChanged(); }
valueChanged: function() {
var theMaxValue = this.get('maxValue');
if (this.get('value') > theMaxValue) { this.set('value', theMaxValue); }

var foo = new MyModel({ value: 5, maxValue: 10 });
var theValue = foo.get('value') + 20;
foo.set('value', theValue); // valueChanged() limits value to 10
theValue = foo.get('value'); // had to re-fetch to get actual current value
It's a bit annoying to have to re-fetch the value though. I'm curious to know whether there might be a better way for me to do this?

Alternatively I'm wondering whether there might be a case to make for a .setGet() or similar method that can both set a value, and return its final actual value once all events are resolved, as it would avoid the need to re-fetch myself.


  • It might be better to override set() to clamp the value. That would avoid a second update since each update has to notify all of its observers.
Sign In or Register to comment.