-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce a "ready" event for views #3243
Comments
I am guessing |
I don't really know how you mean. I guess I am looking for one event that would work for setting up children for a view that is prepopulated, but has a template for rerendering. |
sorry, I mean domRefresh. The suggestion of an But if people need an event strictly to know that the view is ready since the target element is a pre-rendered dom element, then I guess this is good. Would having this event mean not having to trigger other events like before:render and render, since the view's element is already rendered on the page? Or not triggering before:attach and attach. Cutting these events down for this case could help with perf. But are there past issues related to this being an issue? |
I actually see the need in the community for this frequently: #3128 (comment) And I suspect as we better support pre-generated html, we will run into this issue more often. The crux of the issue is:
I think it'd be better to give the users a single event, which by name says, "now you can do stuff"
|
A code example of how this would help: const MyView = Mn.View.extend({
regions: {
'fooRegion': '.foo-selector'
},
initialize() {
if (this.isRendered()) {
// onRender won't fire if the view is already rendered on init
this.triggerMethod('ready');
}
},
onRender() {
// If this view is re-rendered, we will want to setup the children again
this.triggerMethod('ready');
},
onReady() {
const myChildView = new MyChildView({ el: this.$('.bar-selector')[0] });
this.showChildView('fooRegion', myChildView);
},
template() _.template('<h1>Hello World!</h1><div class="foo-selector"></div>'),
modelEvents: {
'change': 'render'
}
});
const $existingEl = $('.some-selector'); // pre-rendered by the server
myApp.showView(new MyView({ el: $existingEl[0] }); And maybe the better solution is to throw the related events for when |
I understand the logic but I'm in favor of using I think everyone get used to use If someone uses a view with any exiting element, then he knows its view |
The moving of one event name to another is precisely why I'm suggesting we abstract it. We've been trying to find and constantly modify what event is best to consider the view "ready" to act on.. and as things have changed in the code, the best practices have changed as well. And now as we're getting new use-cases (server rendered HTML) we're realizing that onRender doesn't really work either.. So I think there's 3 options:
|
I understand but could seems like https://xkcd.com/927/ from Marionette user PoV. |
I would follow the motto "Make the common things easy, rare things possible" Given that is possible, and relatively easy, to handle use cases with current primitives, like the presented above, i would avoid introducing a new event and document the usages as recipes. |
Perhaps Mn is not "ready" for this 🤓 |
Closing for #3128 |
note to self: if we ever implement this a |
I changed my opinion about it and i think is a good feature |
While doing some region work, I definitely think it's time to readdress this |
It's possible this should just wait for v5 since the behavior will be moderately different for nondestructive views.. But we could introduce it, documenting the upcoming breaking change, so that people can migrate to it now and reap the rewards come v5. |
Description
Currently the best place to assume a view is ready to do most things is
onRender
(with the exception of needing to know the view is in the DOM)This is all fine and dandy until you initialize a view with rendered DOM. Then when the view is created it is already rendered, and therefore does not need to be rendered and thus
onRender
would never occur. The current solution is awkward and asks the user to either use "template: false" or re-render the view.. Or in some cases setup view related things insideinitialize
and then potentially later inonRender
as well.We can simplify this by throwing a
ready
event once we know the view is rendered, regardless of where it was rendered.It also way simplifies understanding where to do things.
It doesn't necessarily solve attachment, as a similar problem comes up if the view is already attached.. but I think that's more edge case-y and can be handled by:
The text was updated successfully, but these errors were encountered: