vendredi 11 mars 2016

How to create an Ember computed property that adds 1 to a property?

I'm just learning Ember and I'm a bit confused about computed properties. Everything in the guides uses strings, like a computed property that creates a full name out of a first and last name, etc. Anyway, I'm confused about how to use integers because it seems like the syntax almost demands the use of strings.

As an example, I have a property on my controller called count.

import Ember from 'ember';

export default Ember.Controller.extend({
  count: 0,

  counter: Ember.computed('count', function() {
    var num = `${this.get('count')}`;
    var newNum = parseInt(num) + 1;
    this.set('count', newNum);
    return this.get('count');
  })
});

I know this code is really bad, I'm just trying to illustrate what I'm trying to do and failing at. Why does Ember.computed take a string as its first parameter?

And why do I have to use a string in this.get and this.set when I'm working with an integer, not a string? I have to manually parse the int or else it returns a string, why is it converting my count property into a string?

Anyway, in the ember inspector when I run

$E.get('count')

it does successfully add 1, but fails to continue to add 1, which makes me think its not updating the actual count property.

Thanks very much. I appreciate the help.




Aucun commentaire:

Enregistrer un commentaire