学习Model-Glue框架的系列教程-Building your first Model-Glue Application中文理解通俗版(6)
来源:互联网 发布:短刀极化数据 编辑:程序博客网 时间:2024/06/06 04:30
- <cffunction name="logout" access="public" returntype="void" output="false" hint="I log the user out.">
- <cfargument name="event" type="ModelGlue.Core.Event" required="true">
- <cfset structDelete(session, "loggedIn")>
- </cffunction>
- <cfinclude template="header.cfm">
- Code for the page.
- <cfinclude template="footer.cfm">
- <cfmodule template="/site/customtags/layout.cfm" title="Star Wars is Art">
- Code for the page.
- </cfmodule>
- <event-handler name="Home">
- <broadcasts>
- <message name="getAuthenticated" />
- </broadcasts>
- <views>
- <include name="body" template="dspBody.cfm" />
- <include name="main" template="dspTemplate.cfm" />
- </views>
- <results>
- <result name="notAuthenticated" do="Logon" />
- </results>
- </event-handler>
- <cfif viewCollection.exists("body")>
- <cfoutput>#viewCollection.getView("body")#</cfoutput>
- </cfif>
Basically this code says, check for a "body" field in the viewCollection, and if it exists, display it. Guess what? That's it. Pretty simple, right? And to be honest, I even prefer this to my custom tag approach. Even though my layout custom tag keeps everything in one file, it is still separated by a CFIF branch for each execution mode. In my Model-Glue template, it simply has the code above in the middle. I find this even easier to read.
Your next question is - how do I set a title, or other dynamic aspects, into the template? I've talked before about the viewState. This is like a shopping cart where you can place data to be used during the request.Normally, we have used the controller to set this information. However, a view can do so as well. Consider the logon template. It now has this on top:
- <cfset viewState.setValue("title", "PhotoGallery Logon")>
- <cfset title = viewState.getValue("title", "PhotoGallery")>
This says, look into the view state for a value called title, and if you don't see one, use a default of PhotoGallery. In other words, let's see if an earlier view set a title, and if not, default it to PhotoGallery.
At this point, it may be a good idea to take a look at the sample application and see the layout in action. For those who registered already, your username and password should still work, or you can just use admin/admin. Before ending this entry, I want to talk a bit more about views to give you something to chew on until the next entry.
First off - in my example, I used views with different names. If you want, you can use two, or more, views with the same name. If you do that, normally, the last view will overwrite any previous view with the same name. You can get around this by using append. This example is stolen from the quick start:
- <include name="content" template="dsp.helloworld.cfm" />
- <include name="content" template="dsp.helloworld.cfm" append="true" />
- <include name="main" template="layout.main.cfm" />
In this example, Model-Glue will first run dsp.helloworld.cfm and store it in a viewCollection variable named content. It will then run the template again, but this time the result will be appended to the content variable. Lastly it will run layout.main.cfm, and if it uses "content" from the viewCollection, you will see the HTML from that template twice. To be honest, I've never used this feature and can't imagine doing so. I can see one possible use. You may have multiple pods, for example, and you may want to append them all into a "pod" viewCollection variable. Your layout file would then simply use that variable, and not need to know how many different pods there were.
Lastly, the way I've done the layout is not the only way you can do it. I've talked about using results before. For example, the Home event will fire the Logon event if you are not authenticated. Did you know you can also run an event if no specific value was set in the result? Consider this:
- <result name="DoLogin" do="DoLoginRegister" redirect="yes"/>
- <result do="Layout" />
This logic says, if a "DoLogin" flag was added to the event result, run "DoLoginRegister." The last result, however, will always fire if the previous one does not. In this case, I've actually made a Layout event. Why do this? More complex sites may do a lot of logic in the layout. For example, CFLib shows information in the pods to the right. By using a Layout event, I can handle this a bit easier since I would only need to update one event in case of a change like this. I can say typically I almost always use this approach, but for simplicity's sake, I'm using the approach we have above. As I've mentioned (multiple times now), I plan a "What would I change" type article at the end to discuss possible updates we could do to the application.
So, I mentioned above that I'd actually add a bit of functionality to the site. You may notice that the menu of the site has three options: Home, Photo Galleries, and Logout. The Photo Galleries event doesn't exist yet, but the Logout event was trivial so I added it. Here is the event from the ModelGlue.xml file:
- <event-handler name="Logout">
- <broadcasts>
- <message name="logout" />
- </broadcasts>
- <views />
- <results>
- <result do="Logon" />
- </results>
- </event-handler>
- I then added a logout listener to my controller block:
- <message-listener message="logout" function="logout" />
- And finally added a logout method to my controller:
- <cffunction name="logout" access="public" returntype="void" output="false" hint="I log the user out.">
- <cfargument name="event" type="ModelGlue.Core.Event" required="true">
- <cfset structDelete(session, "loggedIn")>
- </cffunction>
As you can see, this was fairly trivial. In the next session will start working with photo galleries.
That's it for this session! As always, you can play with the live application here:http://pg1.camdenfamily.com. You can also download the application by clicking the "Download" link at the end of this article.
Summary:
- This entry was all about looks. I call this the Paris Hilton entry. It's shallow and stupid, but somehow consider important by the majority of people.
- I talked about views, and how views can be stacked, and use each other in order to create a layout template.
- I then talked about other ways of using views, and about using a layout event instead of simply using another view.
- Lastly I added a simple logout event. By now you should be getting the hang of how features are added. The normal process is:
- Add event to Model-Glue.xml file.
- Update Controller to "speak to" the Model if need be.
- Return data to the event to be used by the View if need be.
- As a quick last note, I disabled debugging in the Model-Glue XML file. I'll discuss this change later on though. I just did it to make the layout a bit cleaner while I was testing.
- 学习Model-Glue框架的系列教程-Building your first Model-Glue Application中文理解通俗版(6)
- 学习Model-Glue框架的系列教程-Building your first Model-Glue Application中文理解通俗版(前言)
- 学习Model-Glue框架的系列教程-Building your first Model-Glue Application中文理解通俗版(1)
- 学习Model-Glue框架的系列教程-Building your first Model-Glue Application中文理解通俗版(2)
- 学习Model-Glue框架的系列教程-Building your first Model-Glue Application中文理解通俗版(3)
- 学习Model-Glue框架的系列教程-Building your first Model-Glue Application中文理解通俗版(4)
- 学习Model-Glue框架的系列教程-Building your first Model-Glue Application中文理解通俗版(5)
- Model-Glue:Flex框架快速入门
- Easy ColdSpring in Model-Glue
- 胶水代码(glue code)的定义(粘合代码)
- Backbone系列:Model的学习
- EF框架—Model-First
- Jena使用教程---RDF MODEL的理解
- Model First
- ORM系列之二:EF(4) Model First
- 胶连逻辑(glue logic)、FPGA里面的jitter,skew含义
- 深度学习框架提供的“Model Zoo“
- 对于model的理解
- 降低编译时间的几条方案
- WebLogic Server Administration Console
- 使用grub4dos引导Linux
- Chrome没有Mac版 Sergey Brin很尴尬
- csdn 写博
- 学习Model-Glue框架的系列教程-Building your first Model-Glue Application中文理解通俗版(6)
- 关闭IIS HTTPRERR日志功能
- SQL Server 2008 Full Text Searching SSMS Demo(SQL2008全文检索演示代码)(来自msdn)
- 传李开复将离开谷歌中国 由刘允接替职位
- VS2008中关于将旧VC方案修改为最新的MFC Feature Pack最需要注意的问题
- 循环队列的链表表示
- Adobe彻底改造Photoshop 推移动版本
- Windows 2008 Roles and Features
- 解析#pragma指令