petermichaux / maria

The MVC framework for JavaScript applications. The real MVC. The Smalltalk MVC. The Gang of Four MVC.
BSD 2-Clause "Simplified" License
764 stars 51 forks source link

Child views selecting where they are appended ... #81

Closed jamesladd closed 8 years ago

jamesladd commented 10 years ago

We would like to be able to tell a parent view where to append a child view with more control.

Currently you have this:

1 maria.ElementView.prototype.buildChildViews = function() {
2    var childViews = this.childNodes;
3    for (var i = 0, ilen = childViews.length; i < ilen; i++) {
4        this.getContainerEl().appendChild(childViews[i].build());
5    }
6 };

Would it be possible to change line 4 to ask the child view where it should be appended and if not specified by the child only then chooses the this.getContainerEl() ?

For example:

4 this.getContainerEl().appendChild(childViews[i].build(), toolbarElement());

This would help us in situations like this:

<div class='a'>
  <div class="column1">
  </div>
  <div class="column2">
  </div>
  <div class="toolbar">
  </div>
</div>

And we want the child view to be in one of 'column1' or 'column2' or 'toolbar' rather than all being siblings inside of 'a' (current functionality)

petermichaux commented 10 years ago

Your situation is something I've thought about. It is not something that I've actually encountered in an application with Maria; however, it is inevitable and I know I will one day. This is why building the child views is isolated to a method buildChildViews. That way the child appending behaviour can be overridden.

I think that a child view shouldn't ever care where it is inserted in a parent. The child shouldn't even know what the parent view is. Somehow the parent should know where to put the child.

I think you should override buildChildViews in your parent view so that the parent can choose where to append children. If you really think that your child views should be asked by the parent where to insert the child views then you can do that in your parent view's overriding buildChildViews.

jamesladd commented 10 years ago

We are not really saying the child should know, rather we want a clean way for the parent to associate a child view with where the child view should be anchored in the DOM.

We will investigate buildChildViews further.

petermichaux commented 10 years ago

Can you share how you overwrote buildChildViews?

petermichaux commented 8 years ago

Please reopen if you'd like to continue the conversation.