Andrew
Andrew

Reputation: 5176

Ember itemController gets reinstantiated when ArrayController reorders

Is this expected behavior?

App.PostsController = Ember.ArrayController.extend({
  itemController: 'post',
  sortProperties: ['date'],
  sortAscending: false
});

App.PostController = Ember.ObjectController.extend({
  init: function () {
    console.log('post controller init');
    this.set('propertyNotIncludedInModel', true);
  }
});

Say I have a list of posts, and I inline-edit one of the post's date. The PostsController will reorder the list. As soon as it does, the console will log that the one whose date changed ran init. Of course, any controller properties (not model properties), are no longer there (I mean, it's a new object, so any property that doesn't have a reason to be there is undefined).

You can see this happening in this jsBin.

This seems horribly wrong. I feel like it breaks any sort of analogy to real life. If I have a bunch of objects sitting on a table and I want to reorder them, I don't destroy the one I want to move and then recreate it. What is Ember's justification for this behavior, and how do I deal with it? I need to set controller properties on an object whose position in an array might change. Even better, I need the views and their components represented by that object to persist and maintain their own states and properties as well—currently didInsertElement is being called on every view/component under the item controller, messing things up more. How do I make sure the properties and subviews I need persist when the object gets moved?

Upvotes: 1

Views: 64

Answers (1)

Aaron Renoir
Aaron Renoir

Reputation: 4391

Its up to you how you apply changes to state. If you are ordering by a property the order will change as soon as the property changes. If you want to wait for something to happen before triggering the reorder I would use the buffered proxy pattern. I edited your jsbin as an example. http://jsbin.com/zopelu/1/

EDIT I missed the part about the controller being destroyed and recreated. I think that bug was introduced in 1.7 and almost fixed

Upvotes: 1

Related Questions