mercredi 26 août 2015

Harnessing "computed properties aren't computed unless something tries to get them"

I'm learning Ember via ember-cli. I have a problem which I believe is related to "computed properties aren't computed unless something tries to get them", but I can't figure out how to "get" the property in this case. I created an app, one controller, one service, and one template using ember-cli:

ember new st
cd st/
ember g controller index
ember g service start
ember g template index

Controller:

import Ember from 'ember';

export default Ember.Controller.extend({
  start: Ember.inject.service(),
  value: Ember.computed('start', function () { 
    return this.get('start').value;
  }),
  actions: {
    update: function() {
      this.get('start').update();
      alert(this.get('start').value);
  }
 }
});

Service:

import Ember from 'ember';

export default Ember.Service.extend({
  value: "original",
  update: function() {
    this.set('value', "updated");
  }
});

Template:

<p>{{value}}</p>
<br>
<button {{action "update"}}>Update</button><br>

Then I serve this with "ember serve". As expected the value shown is the initial value "original". When I click the Update button, I get an alert showing the new value "updated" however the original value stays on the screen. How can I "get" this injected property such that it is computed and therefore updated?

ember-cli is 1.13.8, ember itself is 2.0.1




Aucun commentaire:

Enregistrer un commentaire