| Page 1 of 1 | [ 8 posts ] |
|
Please have a look following example:
I create a Y.View instance and pass a container as configuration option. I also pass events object as configuration option. Then I found that events do not work! I spent a lot of time to find a reason: container is lazy attribute, so events are not attached automatically once I create a new instance of View - this is really confusing and not described in User giude or API docs. Code: Code: //Let us create a View and pass a container selector already exists in markup with some events var myView = new Y.View({container: '#viewcontainer', events: {'a': {click: onClick}}}); //Surprise: View instance is created, but events still do not work! We have to manually call attachEvens method myView.attachEvents(); //Another way to get events work: instead of call attachEvents, call get('container') myView.get('container'); //because container is lazy attribute, here it will be initialized firs time and events will be attached - really not obvious! http://jsfiddle.net/mNLyS/ Am I wrong somewhere? If it's just as planned, I think it should be described in the user guide and API docs. |
|
I'm not sure exactly what you're trying to build, but you can rewrite this a little bit so it works easier.
The first thing I noticed is that you're trying to pass an events object as an attribute. events is actually a property of Y.View and not an attribute. If you want to use this way of setting up custom events, I think you are required to extend Y.View and make your own custom view class. The next thing I noticed is that you are never rendering the view. You have markup that is already on the page. Widgets are made to progressively enhance existing markup but Views like to render content themselves. Views also like to create their own container instead of having one given to them. Here's your example refactored to take these into account: http://jsfiddle.net/mNLyS/1/ |
|
Steven,
thank you for helpful advices, now I more or less undertand how to build by app. Just watn to add a couple comments to summorize the resolution: 1. Quote: The first thing I noticed is that you're trying to pass an events object as an attribute. events is actually a property of Y.View and not an attribute. Here you right that events is a property of Y.View and not an attribute, but in definitelly can be initiaged within config object - I found it in Y.View source in initializer method: Code: // Merge events from the config into events in `this.events`. this.events = config.events ? Y.merge(this.events, config.events) : this.events; http://yuilibrary.com/yui/docs/api/file ... .html#l135 2. Quote: You have markup that is already on the page. Widgets are made to progressively enhance existing markup but Views like to render content themselves. Here you right, Widgets worth it better. But let me explain my application more detail. I write a some kind of Todo Application using classic approach with ModelList, Models and Views. When user is logged in Initial Todo list is rendered on server side (because server side rendering is faster) - that is why I have to initiate my Views after domready on top of existing markup. For that I do not call render method, I just pass a container node into constructor for each View instance. Actully, my application is not so siple as my jsfiddle example - in real code I do extend Y.View, but the issue valid. Someone from #yui (irc) show me following ticket: http://yuilibrary.com/projects/yui3/ticket/2532211 Y.View events don't get attached unless a this.get("container") is called and it's closed won't fix with following resolution: Quote: This is more or less by design. We don't want to attach any events to the container until the view is actually rendered, and in order for the view to be rendered, it must get the container. If you're using a view in such a way that you're not actually rendering it or getting the container after setting it, you can attach events to the container manually by calling the view's attachEvents() method. So solution for my application found: manual call attachEvents() in case of creation from markup. But here I defenitelly see a lack of documentation in User guide! |
|
I can look at making this better and more intuitive when refactoring View, which is planned in the near feature.
In the mean time, if you think the documentation should be changed, please feel free to change it and issue a pull request to our `live-docs` branch. |
|
It's cool that Y.View is smart about events in the config object; I was not aware of that. I see you definitely have a valid use case but it seems Y.View doesn't do a great job addressing it. I'm glad you've found a way to make it work.
ericf: has there been any thoughts (or implementations) of progressive enhancement for views? I've seen it come up a few times. I'm thinking there could be a class extension or plugin which implements widget's render/bind/sync methods. |
|
Steven,
We definitely want to keep View light, and not got down the road of Widget. There are already a couple tickets open for this, and something I will be looking at when working on View for 3.7.0. The most likely thing is that we'll simply attach the `events` if a view is constructed with a `container`. |
|
That sounds like a good solution for Y.View.
|
|
Long time listener, first time poster (first time building with YUI, actually)....
Having wrestled with the problem the original poster was facing, I realize now that Y.View very nicely handles events within its scope, i.e. the scope of the container. No container, no events. So, it appears to me that by definition, Y.View needs to change in this case, just for the docs to point out this subtle, albeit important, point. As an aside, having realized this, I'm now laying out the UI for my app to take advantage of this by embedding Views into an overall container View, and then laying in the event registries where they make sense. It allows each view to remain singular in its purpose, and keeps me from having to proliferate code such as container.one('someSpecification') just to get to the scope of the specific View. Regards, Max |
| Page 1 of 1 | [ 8 posts ] |
| 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 |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group