lundi 22 juin 2015

ember-data store string instead of number

In my ember app I want to reuse a model attribute as soon as the form is submitted. But the store seems to keep it as string unless I reload the whole route.

Ember      : 1.12.0
Ember Data : 1.0.0-beta.18
jQuery     : 1.11.3

/app/controllers/ledger/purchases/new.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function () {
    return {
      newPurchase: this.store.createRecord('purchase', {
        name: null,
        amount: null,
        player: null
      })
    }
  }
});

/app/templates/ledger/purchases/new.hbs

<div class="row">
  <div class="col-xs-12">
    <h4>New purchase</h4>
    <form>
      <div class="form-group">
        <label for="name" class="sr-only control-label">name</label>
        {{input id='name' type="text" value=newPurchase.name placeholder="What" class="form-control"}}
      </div>
      <div class="form-group">
        <label for="amount" class="sr-only control-label">amount</label>
        {{input id='amount' type='number' value=newPurchase.amount placeholder="How much" class="form-control"}}
      </div>
     <div class="form-group">
        <button type="submit" class="btn btn-success" {{action "create"}}>create</button>
        {{#link-to 'ledger.purchases' tagName="button" class="btn btn-link" }}cancel{{/link-to}}
      </div>
    </form>
  </div>
</div>

/app/controllers/ledger/purchases/new.js

import Ember from 'ember';

export default Ember.Controller.extend({
  newPurchase: Ember.computed.alias('model.newPurchase'),

  actions: {
    create: function() {
      var np = this.get('newPurchase');
      console.log(Ember.typeOf(np.get('amount')));
      ........
      save np etc...
    }
  }
});

the console log call clearly shows that the type is a string. The ember inspector shows the same. However data are correctly saved to the backend because after reloading everything is fine. But I need the amount as a number as soon as it is submitted because I use it to make and show the sum of all purchases.




Aucun commentaire:

Enregistrer un commentaire