-
Notifications
You must be signed in to change notification settings - Fork 27
Provide subviews directly when rendering instead of via globals #17
Comments
I've been thinking about something like this for helpers as well, I don't like the fact that they are global. Local helpers would be very useful, we could even use them for rendering views. And getting away from having to define controllers and views globally would be super. var view = Serenade.view(tmpl);
var element = view.render(model, controller, {
myView: function () {
return otherView.render(this.model, myOtherController);
},
anotherHelper: function() {
return document.createElement('div')
}
}); in view:
|
Not sure if we should only have local helpers though, or if there should still be some way of defining them globally? |
You're refering to View helpers is a very good name for this feature. A view has a model, a controller, and optionally a helper. Makes sense! I personally never found the global views useful. I think it's better if it's the programmers responsibility to keep a global reference to a view. Something like:
Most apps have a namespace setup like this anyway, and it lets Serenade avoid having a private black box of views stored somewhere. A little harder, but I think the end result has more clarity, more simplicity, and less magic. Also, the presence of view helpers makes the internal name based references useless in my taste. I'd much rather have my own plain reference to a view, and plainly call it in a helper, than having a named internal reference in my templates, as it is today. If the built-in global views are removed, it might anger people that has grown used to them though :) So perhaps it makes sense to keep the global views, at the very least as a deprecated feature. |
Added an implementation of this, see https://github.com/augustl/serenade.js/tree/view-helpers. https://github.com/augustl/serenade.js/commit/e6132d315f65efc51e3c0a3d7a24d36997a26abd is an unrelated fix, perhaps I should put it in a separate branch/issue? |
In my experience, the extra helper object is not really needed. The methods might as well get called on the controller. |
Currently, a view needs to be made globally available with
Serenade.view("my_view", tmpl)
when subviews are used. This also puts some restrictions on what the controller can be - it currently has to be a constructor, for example.It would be better if the subviews could be provided to
aView.render
.In the template we have
- view myView
. This will cause myView to be called on the 3rd argument passed torender
. This function is assumed to return a DOM element, such as the DOM elementsrender
on another view will provide.I think having a 3rd argument for this is better than using the controller, since view names might conflict with what you otherwise have on your controller.
The
model
passed would be the current model, be it for the template root or the current collection model.The text was updated successfully, but these errors were encountered: