Evaluate "augment" implementation of extending Marionette #282

Closed
wants to merge 30 commits into
from

Conversation

Projects
None yet
4 participants
Member

jsoverson commented Oct 12, 2012

This proposed implementation allows Marionette Views to be extended and composed of individual addons and mixins (currently referred to as 'augments' or 'augmentations').

An example of functionality breaking down Marionette core is abstracting the login in Layout to a RegionManager augment that can be implemented like so:

Marionette.Layout = Marionette.ItemView.augment(
  Marionette.augment.RegionManager
);

This, alone, allows the region functionality available normally only in Layout to be used in any other view, Marionette core or otherwise:

var CompositeLayout = Marionette.CompositeView.augment(
  Marionette.augment.RegionManager
);

It also allows for chains of augmentations that can make combinations that may not have been previously planned on, an example shown in the repo marionette.augment.inlineregions

var MyView = Marionette.ItemView.augment(
  Marionette.augment.RegionManager,
  Marionette.augment.InlineRegions
);

The code for .augment() inverts the relationship between the views, allowing the augment to alter the caller and return the extended calling view. In the examples given this is done to essentially just implement a reverse .extend() but the extending functionality is not set in stone. This was intentional to just provide an avenue for extension but allow for things we did not plan for.

// A class method in Marionette.View
  augment : function(/* augments... */) {
    var self = this;
    for (var i = 0; i < arguments.length; i++) {
      var aug = arguments[i];
      if (aug.augment) {
        self = aug.augment(self);
      }
    }
    return self;
  }

I'm open to alternate naming, "augment" is the first reasonable option I came across.

Member

jsoverson commented Oct 12, 2012

Documentation is entirely lacking. This PR is for comments on how valid the approach is and the documentation on this change would be substantial and can be done in pieces.

Owner

derickbailey commented Oct 12, 2012

I have a lot of thoughts and ideas about this, and they're rather unorganized. So here's kind of a big brain-dump of what I'm thinking. :)

I definitely like the direction this is heading in terms of what it facilitates. this is something that i get asked about a lot, actually. one of my clients is using really simple mixins with _.extend, a friend of mine just yesterday was asking about how to do mixins, someone on IRC was asking about them recently, etc. people are definitely looking at using the building blocks of marionette's functionality and composing their own views and their own behaviors as mixins. this needs to make it in to marionette, in some form, to make all of that possible.

i've already been thinking about this a lot and some of this has influenced the directions that was moving with the 1.0.0-beta1 release... specifically, extracting more functions out in to the Marionette object directly, for re-use; and also how i set up the use of the EventBinder with a function to add it in to any other object.

if you haven't read it yet, i wrote a blog post on some basic ideas of mixins and avoiding some of the pitfalls that a simple "extend" method can introduce: http://lostechies.com/derickbailey/2012/10/07/javascript-mixins-beyond-simple-object-extension/ - this is something that we need to look at incorporating in to the augment feature. this turned in to an idea for a javascript mixin system in general as well... and i couldn't resist the project name / tag line for it: https://github.com/derickbailey/catalinajs :D perhaps this could become the basis for marionette's augment? or maybe i'm thinking waaaaay to far head. but I've also been thinking that this would be applicable to more than just Marionette object, as well. It could easily be a plugin to allow standard Backbone object to be augmented. It could be a dependency like EventBinder / Wreqr.

anyways... +1 for this direction. this is going to be awesome.

Member

jsoverson commented Oct 12, 2012

It could easily extend to a more generic implementation that allows for usage on any backbone style object (or further). I read your post and think that solution also adds benefit but probably even on top of this or maybe as a complement to this to make writing extend-style augments easier or more intuitive to write.

This deliberately simple implementation adds open ended functionality that could do just about anything between two objects, not just mixin or override methods. A contrived, off the cuff example (that probably isn't actually useful) is to supply an augment that binds the context of all methods of an implementer to something else.

The openness was my goal, mostly motivated at the creative ways I'm seeing people use Marionette already. Giving people a supported outlet to hook into is a good way to find some awesome new ideas.

+1 to implement this in any form. I finally get it. I found myself needing
this functionality this morning, which caused a light bulb to go off. :)

On Fri, Oct 12, 2012 at 7:19 AM, Jarrod Overson notifications@github.comwrote:

It could easily extend to a more generic implementation that allows for
usage on any backbone style object (or further). I read your post and think
that solution also adds benefit but probably even on top of this or maybe
as a complement to this to make writing extend-style augments easier or
more intuitive to write.

This deliberately simple implementation adds open ended functionality that
could do just about anything between two objects, not just mixin or
override methods. A contrived, off the cuff example (that probably isn't
actually useful) is to supply an augment that binds the context of all
methods of an implementer to something else.

The openness was my goal, mostly motivated at the creative ways I'm seeing
people use Marionette already. Giving people a supported outlet to hook
into is a good way to find some awesome new ideas.


Reply to this email directly or view it on GitHubhttps://github.com/marionettejs/backbone.marionette/pull/282#issuecomment-9378004.

Hi! I'm new to the project and I'm really impressed by the quality so far, I hope I can contribute!
Being an ExtJS user for several years I can say how important a way of adding/modifying behaviours without extending is important to views during a project lifetime - they use Plugins. I would open a new branch and implement this for sure, looking carefully for existing solutions and concepts in order to achieve a simple, beautiful and effective solution. BTW, great post Derick!
This feature is even more important when thinking of views as widgets, as someone already said in other topic.

Member

jsoverson commented Oct 21, 2012

Closing this and releasing separately : Backbone.Augment

It's not necessary to tie it into Marionette and, if something like RegionManager is deemed valuable, Backbone.Augment can be included as a Marionette dependency.

@jsoverson jsoverson closed this Oct 21, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment