In my last post I talked a little about getting started with scaffolds in Model-Glue Unity. Anyone you talk to will agree that scaffolds are a great way to see something come together fast, and can be a good tool for developers. Not many will say that they are all you need to build sites, and some will even say they just get in the way.
Well I happen to believe they are a great place to start. So much so that when I had to build a free site for someone recently I used MGU in the admin, even though the front end of the site was done with fusebox. This arrangement worked out great for a while, but now they need a little bit more information to be displayed. MGU makes it very easy to take what it builds and customize that. For starters I wanted to modify what gets displayed on the member.list page of my admin. Well MGU generates the templates for me inside the /site/views/generated folder. For the purposes of displaying the list of members the file is named dspMemberList.cfm. If you just take that generated file and drag it, up out of the generated folder, into the views folder MGU will use this file instead of the generated one. So by default the scaffolds in MGU display every field in your table across the page, and we dont need all that information. The first task is to only display the data that we need. Remember this is just a summary page. After saving your new file with you modifications you can run your page again to see how you have updated the layout. You can also do this with the frmMember.cfm and dspMember.cfm files if you would like to modify the layout of them.
That is all well and good for getting started on making modifications. But lets look at something a little bit harder. And I will note here that I am using MGU from the svn respository ( its possible this stuff may not work with whatever release is out in beta). On my member list page I want to display some aggregate data. Maybe its a sum of the number of times a user has logged in. But really the point is just that our generic query that the scaffolds wrote for us isnt going to cut it anymore. What I really need is to write my own query and use that to populate my display. This task is almost as simple as creating the scaffold in the first place. First thing we want to do is get the generated event handler xml code from the scaffolds.xml file. This will be located at /site/config/scaffolds/Scaffolds.xml assuming site is the name of your MGU app and you haven’t changed the location of the scaffolds info in your MGU configuration. Glancing at that file you will find a section that looks much like this.
We want to copy that code and paste it into your ModelGlue.xml file, inside the section. Just having this code here is not enough though. We also need to tell MGU not to scaffold the view piece for us. So we need to modify our scaffold tag for member to specify that information.
Now our scaffold knows not to generate the member.list event. If we run our application again and append the &scaffold=true attribute. We should see our application run, just like we left it. Only difference is behind the scenes its using our new xml config in the Model-Glue.xml file.
So lets push ahead. This is not really a tutorial on using MG, I am really just working on extending the scaffolding features. So when I say “I next created a new message-listener and called a method on my controller which got my new query” I am not really going to get very deep into how that works. I believe this to be a standard Model-Glue item and if you have done any you will know how. And if not then check the documentation because Joe does a great job with that. In fact you can take a look at Quickstart 4 in the Model-Glue Unity Documentation to see the steps involved.
So I created a new message-listener called “member.getAllMembers” on my controller and asked it to call the “getAllMember” function on my controller. That function does some stuff to get a query that is joined with a couple tables and does some calculation. Then it puts that query into the event using setValue. Now i need to modify the event-handler for member.list to call this new bit of code and use it for display. This is as simple as changing:
To this new code
Now when we run our ?event=member.list page in our MGU site, our new query is used to get the data. And using the same file we modified above we can display the new data from the query on our list.
Pretty neat. What we looked at here was modifying the code generated by the Scaffolds in our Model-Glue unity application. We created new templates to use for display as well as changed how the code runs. We now have a site that is customized for us, but we still had to write very little actual code to get it done. So go get some coffee and play some Gears of War. You earned it.