Could someone help me with viewlets? I was trying to implement a viewlet on my ColdBox-based app. Basically, I followed the steps explained on http://wiki.coldbox.org/wiki/Layouts-Views.cfm#Viewlets_(Portable_Events). When I ran it, the existing layout that calls the viewlet was not displayed. Instead, it got replaced by the viewlet.
Enviroment:
Windows 7 Professional
Railo 4.2.1
ColdBox 3.8.1
Code: /handlers/viewlets.cfc
`
component {
function home(event, rc, prc) {
event.setView(“viewlets/home”);
}
When you call a handler with runEvent() and use event.setView or renderView() you can end up with views that you don’t want.
If you want to do something like this, create an API that is only accessible to anything that isn’t going to set a view, then refactor your code to do something like this.
Also it might help to think of viewletts like widgets, no matter what the widgte becomes the container and then they do Ajax calls to get the data they need, viewletts are the same they already contain the view all they need now is to retrieve the data, either via models or other events.
It is also maybe good practice to refactor your code in this manner, especially if you plan to support Restful or other RPC type calls, that would allow you to keep your code duplication to a state of zero duplication.
You know, I can’t actually see anything wrong with that!
Luis, Brad & Joel might need to pipe in here. My guess something has changed since the docs where written, could you tell us what version of ColdBox you’re using?
All I have done, is remove the code that is going to be duplicated to a separate method/function and called it in the handler above. Then the method looks like this, it is bare bones but has enough to understand it.
function wrapperToGetUserInfo(event, rc, prc) {
prc.userinfo = wrapperToGetUserInfo();
Run the following commands from the interactive shell to download and and install my viewlet sample app and run it on your local machine (no web or CF server setup is necessary)
mkdir testsite
cd testsite
install coldbox-viewlet-sample
start
Thanks for the complete viewlet sample code. When I compared mine with yours, the only difference is you use composition in the bootstrapper; and mine used inheritance. So, the viewlet is working properly on with composition, but not with inheritance.
Hi Laksma, that shouldn’t make any difference at all. In fact, I just tested it using inheritance and my example site still runs fine. You can do it yourself. Just edit the Application.cfc and replace the contents of the entire file with this text:
component extends=‘coldbox.system.Coldbox’ { }
If you code is behaving differently, there must be something else different with your code. Perhaps, you can create a sample application of your own that exhibits this behavior so we can see it.
Ahh, you’re on ColdBox Lite-- that makes sense. Your behavior definitely seems like a bug, but to be honest ColdBox Lite has now gone away since ColdBox 4.0 is the new stripped-down core and everything has been extracted into modules. If I were you, I would just change the bootstrap over to use the full ColdBox framework.