user2192333
user2192333

Reputation: 139

How do i make each child objectController observe a change in its parent arrayController

I have a parent resource and a child route: addresses and address

A child addressController (a modelController) observes its parent addressesController (arrayController) attribute 'saveRequested'; it then runs its internal action of this.model.save() when the attribute changes to true.

In the addresses template, addresses link-to address, so only one address is active (in an outlet in addresses) at any one time. When the user triggers the parent saveRequested=true;, only the active address observes and perform the required action. Although I was not expecting this behaviour, it does make sense that only one of the child routes is active at one time.

My question is, how do I manage this scenario so that each child address, loaded into memory and stored in the parent addresses arrayController, all respond to the parent save request? Reason: the user makes all the edits he wants of each address and then is supposed to be able to click Save in addresses to then save/persist all the changes made to each address at once.

My only solution thus far is to iterate (forEach) through each address stored in the content of addressController and call the returned addressModel.save() directly. The forEach returns the record/model, not the addressController (objectController) as I was expecting but now I sort of realise why. It works but to me this feels like i'm going through the back door. Do I need each address to be in its own outlet so that all are active and can observe at the same time?

Thank you for any help.

Upvotes: 1

Views: 298

Answers (1)

edpaez
edpaez

Reputation: 1583

I did not quite understood your question. I, however, know each itemController has a property parentController pointing to its ArrayController. You can use this with an observer to know when the parent controller changed:

ItemController = Ember.ObjectController.extend({

    parentControllerDidChange: function() {
        ...
    }.observes('parentController.saveRequested')

});

Does it help you?

UPDATE: See comments below about assigning the target objectController to the arrayController's itemController.

Upvotes: 1

Related Questions