Component tree: controller bindToParent? #14060
Description
Hello. I am thinking about having an application as a tree of components with 1.5.
Let's say we have:
<user-crud> // $crud controller
<user-list users="$crud.users" on-select="$ctrl.editUser"></user-list>
<user-editor user="$crud.user" on-save="$ctrl.saveUser"></user-editor>
</user-crud>
If we want to reuse somewhere else, we should not depend on user-crud. Also, we want to reduce or eliminate the $scope and $watch usage as a good practice.
So, what i miss is a better clean way of communication from parent to its child components (crud to the list and editor) other than using events or directive require.
function UserCrudController(UserService) {
var self = this;
...
self.editUser = function(user) {
// So now i want to tell editor to edit this user:
//1
self.user = user; // user-editor needs to $watch for this
//2
$scope.broadcast('editUser', user); // both UserCrudController and UserEditControllers need to use $scope
// Want to command our child here:
self.userEditor.edit(user);
}
self.saveUser = function(user) {
// same for save
// do some saving logic then:
self.userEditor.hide();
var updatedUserList = ...;
self.userList.update(updatedUserList );
// or maybe
self.$components.userEditor(...);
self.$components.userList(...);
}
}
I believe this approach is not absurd, but it's not the way we used to code it using Angular. Maybe there could be something as a 'bindToParent' property to expose child controller into parent:
<user-crud> // $crud controller
<user-list users="$crud.users" on-select="$ctrl.editUser" var="list"></user-list> // $crud.list==UserListController
<user-editor user="$crud.user" on-save="$ctrl.saveUser"></user-editor> //$crud.userEditor==UserEditorController
</user-crud>
module.component('userList', {
...
bindToParent : true // will bind component name // bindToParent : 'list' // bindToParent : function($attrs) { return $attrs.var; }
...
});
The problem i see with this approach is what could happen if we have a multiple user-list elements or a iteration of them inside an element.
What do you think?