Understanding DS Model life cycle


#1

I am trying to monitor the state changes on my DS Models.

I have found interesting flags in the documentation:

And they work properly in the templates, I can play with the {{#if model.isLoading}}Is Loading{{/if}}, for example.

But what is very disturbing is that the observers in these flags have a very random behavior. For example I have found that the observer over the flat isSaving doesn’t work at all. But the observer over the flag isReloading works perfectly.

Here I have a full example of what I’m have tried until now. I have included this Mixin into my Model:

// app/models/mixins/loading-monitoring
import Ember from 'ember';

export default Ember.Mixin.create({
  isReloadingDidChange: Ember.observer('isReloading', function() {
    console.log('LOG: isReloadingDidChange', this.get('id'), this.get('isReloading'));
  }),

  isSavingDidChange: Ember.observer('isSaving', function() {
    console.log('LOG: isSavingDidChange', this.get('id'), this.get('isSaving'));
  }),

  isLoadingDidChange: Ember.observer('isLoading', function() {
    console.log('LOG: isLoadingDidChange', this.get('id'), this.get('isLoading'));
  }),

  isLoadedDidChange: Ember.observer('isLoaded', function() {
    console.log('LOG: isLoadedDidChange', this.get('id'), this.get('isLoaded'));
  }),

  onInit: Ember.on('init', function() {
    console.log('LOG: init', this.get('id'));
  }),

  onDidLoad: Ember.on('didLoad', function() {
    console.log('LOG: onDidLoad', this.get('id'));
  }),
});

And I try to invoke all these observers but only some ones are triggered:

> $E.toString()
  "<reports-dashboard-client-app@route:application::ember1553>"
> var report; $E.store.findRecord('report', 14).then(function(r) { report = r });
  Promise {_id: 438, _label: undefined, _state: undefined, _result: undefined, _subscribers: Array[0]}
  LOG: init 14
  XHR finished loading: GET "http://myapi.com/reports/14".
  LOG: onDidLoad 14
> report.toString()
  "<reports-dashboard-client-app@model:report::ember1554:14>"
> report.get('title')
  "new title"
> report.set('title', 'title modified')
  "title modified"
> report.save()
  Class {__ember1453996199577: null, __ember_meta__: Meta}
  XHR finished loading: PUT "http://myapi.com/reports/14".send 
> report.reload()
  LOG: isReloadingDidChange 14 true
  Class {__ember1453996199577: null, __ember_meta__: Meta}
  XHR finished loading: GET "http://myapi.com/reports/14".send
  LOG: isReloadingDidChange 14 false

What am I missing?


Monitoring pending AJAX requests
#2

Post in StackOverflow: http://stackoverflow.com/q/35139936/316700


#3

Added a bounty in SO: http://stackoverflow.com/questions/35139936/emberjs-understanding-ds-model-life-cycle