gokatz
9/22/2016 - 11:00 AM

Promise on Init

Promise on Init

{
  "version": "0.10.5",
  "EmberENV": {
    "FEATURES": {}
  },
  "options": {
    "use_pods": false,
    "enable-testing": false
  },
  "dependencies": {
    "jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
    "ember": "2.8.0",
    "ember-data": "2.8.0",
    "ember-template-compiler": "2.8.0",
    "ember-testing": "2.8.0"
  },
  "addons": {}
}
<h1>Welcome to {{appName}}</h1>
<br>
{{myService.test_a}}
{{myService.test_b}}
{{myService.test_c}}
<hr>
SUM: {{totalSum}}
<br>
{{outlet}}
<br>
<br>
import Ember from 'ember';

export default Ember.Service.extend({
  init() {
  	return new Ember.RSVP.Promise((resolve, reject) => {
      //this.set('obj', {a:1, b:2, c:3});
			Ember.run.later(() => {
        this.set('obj', {a:1, b:2, c:3});
      	return resolve;
      }, 2000);
    });
  },
  
  test_a: Ember.computed('obj', function() {
    console.log('a');
    let obj = this.get('obj') || {};
    return obj['a'] || '';
  }),
  test_b: Ember.computed('obj', function() {
     console.log('b');
    let obj = this.get('obj') || {};
    return obj['b'] || '';
  }),
  test_c: Ember.computed('obj', function() {
    console.log('c');
    let obj = this.get('obj') || {};
    return obj['c'] || '';
  }),
});
import Ember from 'ember';

const {inject: {service}} = Ember;

export default Ember.Controller.extend({
  appName: 'Ember Twiddle',
  myService: service(),
  totalSum: Ember.computed(function() {
var a =  this.get('myService.test_a');
    var b = this.get('myService.test_b');
    var c = this.get('myService.test_c');
    return a+b+c;
  })
});