Custom lifecycle plugins for WireJS (or other ideas)

23 views
Skip to first unread message

eschwartz

unread,
Feb 21, 2014, 1:11:24 PM2/21/14
to cuj...@googlegroups.com
Is it possible to author a plugin for a custom lifecycle event?

Use case:

I have a Controller class which fires a 'render' event when it's view is rendered. I would like to wire my controller to do something once it has been rendered.

{
  create
: {
   
module: 'Controller'
 
},
  render
: 'drawAPrettyPicture'
}

Going further, I would like to take advantage of the render lifecycle when authoring plugins. I have a `listenTo` facet plugin, which binds events to a specified eventhub:

{
  create: {
    module: 'ListeningController'
  },
  listenTo: {
    talkerController: {
      'talk:before': 'openEars',
      'talk': 'payAttention',
      'talk:after': 'closeEars'
    }
  }
}

The issue is that I do not want my controller listening to events before the have been rendered. I'm thinking I could adjust my `listenTo ` plugin to only bind events after the `render` lifecycle.

I'd also be interested in other solutions to the same problem, or different perspectives on the pattern I'm using.

Thanks

Brian Cavalier

unread,
Feb 21, 2014, 9:54:30 PM2/21/14
to cuj...@googlegroups.com
Hey eschwartz!

Great question.  From your examples, it seems like the best would be to create a wire plugin that defines one or more wire facets.  A facet is basically a way to do some work on a component (or multiple components) during a particular part of that component's lifecycle.  That's an important distinction: facets represent behavior, and they execute during a particular lifecycle step.

For example, the `properties` facet runs during a component's `configure` lifecycle step.

So, one approach you might look into is to create a `render` facet and a `listenTo` facet that execute during appropriate lifecycles steps.  It might make sense for `render` to execute during an early lifecycle step, such as `create:after` (there are two pseudo stages, :before and :after for each lifecycle step, except create ... there is no create:before), and for `listenTo` to run during `connect`--most things that make connections, like the `connect` facet from wire/connect execute during `connect`.

In fact, you might take a look at wire/connect for ideas of how to implement `listenTo`.  Keep in mind that a single wire plugin can define many facets, so if it makes sense for your use case, you could define both `render` and `listenTo` in the same wire plugin ... or in separate ones.  Again, it just depends on what makes sense for your particular situation.

Here are some links that might be helpful:




Hope that helps!
Reply all
Reply to author
Forward
0 new messages