dgeb/ember_data_example

Memory leak in EditContactController?

Closed this issue · 1 comments

https://github.com/dgeb/ember_data_example/blob/master/app/assets/javascripts/app/controllers/edit_contact_controller.js#L30

Does the Observer set up here need to be removed at some point?? I'm wondering how to improve the structure so that the Observer is declared in the class definition with .observes() and so Ember will automatically handle that.

dgeb commented

Thanks for pointing that out. It was sloppy of me to not clean up that observer. I just pushed a change that does the clean up in the showRecord method:

https://github.com/dgeb/ember_data_example/blob/master/app/assets/javascripts/app/controllers/edit_contact_controller.js#L39

Considering that we only want the observer active until the record is materialized, the current approach is pretty focused. You could achieve the same effect with observes, like this:

  updateRecord: function() {
    // TODO - validations

    // commit and then clear the transaction (so exitEditing doesn't attempt a rollback)
    this.transaction.commit();
    this.transaction = null;

    if (!this.get('content.isNew')) {
      this.showRecord();
    }
  },

  showRecord: function() {
    if (this.get('content.id')) {
      App.router.transitionTo('contacts.contact.index', this.get('content'));
    }
  }.observes('content.id')