Skip to content

Instantly share code, notes, and snippets.

@jakobdamjensen
Created June 30, 2014 12:36
Show Gist options
  • Save jakobdamjensen/33779f6e72a70b70572a to your computer and use it in GitHub Desktop.
Save jakobdamjensen/33779f6e72a70b70572a to your computer and use it in GitHub Desktop.
if( Meteor.isClient ){
var step = function(e, direction) {
var toStep;
e.shiftKey ? toStep = 10 : toStep = 1;
if(direction === 'down') {
toStep = toStep * -1;
}
return validValue.call(this, Number(this.value) + toStep);
};
var validValue = function(val) {
if(val >= 100){
return val = 100;
}else if(val <= 0){
return val = 0;
}
return val;
};
Template.input.created = function() {
this.data.valueDep = new Deps.Dependency();
};
Template.input.helpers({
value: function(){
UI._templateInstance().data.valueDep.depend();
return UI._templateInstance().data.value;
}
});
/*
Template.input.rendered = function() {
var _this = this,
$input = $('input[type="text"]'); //
// Set up a Deps on the input's value
Deps.autorun(function() {
if($input.is(':focus')) {
var selectionStart = $input[0].selectionStart,
selectionEnd = $input[0].selectionEnd;
$input.val(_this.value);
// Reset (i.e. don't move) the cursor position
$input[0].setSelectionRange(selectionStart, selectionEnd);
} else {
$input.val(_this.value);
}
});
};
*/
Template.input.events({
'keydown input' : function(e) {
e.preventDefault();
var that = this; //preserving context for input binding
switch ( e.which ) {
// up arrow
case 38:
that.value = step.call(that, e, 'up');
that.valueDep.changed();
break;
// down arrow
case 40:
that.value = step.call(that, e, 'down');
that.valueDep.changed();
break;
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment