lundi 1 juin 2015

How to create promise aware numeric input

The following is what I have so far. The problem is that if number-input is bound to an async ember-data model property (which will return a promise the frist time an resolve later) - the code below does not work and displays a zero instead.

How can I get it promise aware?

USAGE:

{{number-input numberValue=model.vehicleService.cost}}

CODE:

import Ember from 'ember';

var DELETE = 46;
var INSERT = 45;
var ZERO = 48;
var NINE = 57;

export default Ember.TextField.extend({
  tagName: 'input',
  type: 'number',

  numberValue: function(key, value) {
    if (arguments.length === 1) {
      var number = parseFloat(this.get('value'));
      return isNaN(number) ? 0 : number;
    } else {
      return this.set('value', (value !== void 0 ? '' + value : ''));
    }
  }.property('value'),

  didInsertElement: function() {
    return this.$().keypress(function(key) {
      if ((key.charCode !== DELETE) && (key.charCode !== INSERT) && (key.charCode < ZERO || key.charCode > NINE)) {
        return false;
      }
    });
  }
});




Aucun commentaire:

Enregistrer un commentaire