[ 6 posts ]

Daniel Ji

  • Username: humblepie
  • Joined: Tue Feb 28, 2012 10:30 am
  • Posts: 147
  • GitHub: humblepie
  • Gists: humblepie
  • Offline
  • Profile

custom event communication between Y.Views

Post Posted: Mon Jul 16, 2012 4:09 pm
+0-
Hi everyone,

I've been using the Y.Model and Y.Views extensively throughout my project. It's been working out well so far, but I'm re-evaluating a design choice I made upfront.

At the project's inception with no prior YUI experience, I was pretty overwhelmed with learning YUI and the App Framework. So I decided to have the Y.Views communicate with each other with custom events (which is good), but also made the design choice to have all events be registered/fired on the Y instance.
Code:
Y.fire('myCustomViewEventBroadcastedOnMyYInstance')
...
Y.on('myCustomViewEventBroadcastedOnMyYInstance', ...)

At the time, I had enough understanding and forethought to not put it on the global YUI instance, fortunately. But for the time being, all custom events are funneled through my single Y instance.

I know I can setup my Views to have event targets/chains, but I'm wondering now, is it worth the extra code?

The obvious downside to having all events on the Y instance is that there could be naming collisions for the event names. But I've been using naming conventions, which have been solving that problem.

Should I take the time to specify event chains/targets on the Views? Will there be a performance bottleneck eventually if I continue to register events on the Y instance?

Any feedback would be appreciated.

Thanks.

Alberto Santini

YUI Contributor

  • Offline
  • Profile
Tags:

Re: custom event communication between Y.Views

Post Posted: Mon Jul 16, 2012 10:09 pm
+0-
Hello Daniel.

I think your approach is feasible.

See also how to structure a Y.App:
http://yuilibrary.com/yui/docs/app/#flow-control

It depends on the size of the app.

Regards,
IceBox

Simon Højberg

YUI Contributor

  • Username: hojberg
  • Joined: Thu Aug 04, 2011 10:16 am
  • Posts: 2
  • Location: Providence, RI
  • Twitter: shojberg
  • GitHub: hojberg
  • Gists: hojberg
  • IRC: hojberg
  • Offline
  • Profile
Tags:

Re: custom event communication between Y.Views

Post Posted: Tue Jul 17, 2012 12:30 am
+0-
I usually take the approach of having a Mediator View that listens for events on other views rather than on the Y instance. The Mediator then acts on the event, calling into other views it knows of if necessary. It sits in the back and controls flow and communication. It's a lot like Y.App, only used for a group of related views.

Daniel Ji

  • Username: humblepie
  • Joined: Tue Feb 28, 2012 10:30 am
  • Posts: 147
  • GitHub: humblepie
  • Gists: humblepie
  • Offline
  • Profile
Tags:

Re: custom event communication between Y.Views

Post Posted: Tue Jul 17, 2012 10:34 am
+0-
Hi Alberto & Simon,

I've been thinking about using Y.App to be the listener, but since my project does not require the Router, I'd only be using Y.App as a 'controller of sorts' for forwarding custom events fired by the views. In which case, the mediator view idea sounds more in line with what I'd need.

Right now, I register the custom events in the initializer(). So basically all that would change is this registration would occur in the mediator view and I'd need to setup a event target chain b/t the view(s) and the mediator view?

I'm not entirely sure how this would work out. Is there a sample code to demonstrate this Mediator View?

Thanks.

Simon Højberg

YUI Contributor

  • Username: hojberg
  • Joined: Thu Aug 04, 2011 10:16 am
  • Posts: 2
  • Location: Providence, RI
  • Twitter: shojberg
  • GitHub: hojberg
  • Gists: hojberg
  • IRC: hojberg
  • Offline
  • Profile

Re: custom event communication between Y.Views

Post Posted: Wed Jul 18, 2012 4:40 am
+0-
Daniel,

Here is an example of a mediator view: https://gist.github.com/3135950

In many cases however, I find that the best communication between views is when they share models. Often changes on models are what needs to update other views. I think I only use event handlers for views very rarely - I still use mediator views to glue together views and models for more granular sections of a page that is to specific for an Y.App instance to care about.

Daniel Ji

  • Username: humblepie
  • Joined: Tue Feb 28, 2012 10:30 am
  • Posts: 147
  • GitHub: humblepie
  • Gists: humblepie
  • Offline
  • Profile

Re: custom event communication between Y.Views

Post Posted: Wed Jul 18, 2012 10:30 am
+0-
Hey Simon,

Thanks for the code snippet and your insight. There's a few different ways to organize my code in light of this discussion. I'll have to let it ruminate for a bit :)
  [ 6 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum