`
laowang
  • 浏览: 37492 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

hellolift学习笔记(4)

阅读更多
5.Entry相关的sitemap
由于User相关的内容有很多是由框架缺省实现和控制的,所以先跳过,来看看由应用控制的Entry相关的内容。首先还是来看他的菜单定义。
在hellolift\src\main\scala\com\hellolift\model\Entry.scala可以看到,定义形式有了新的变化
  // sitemap entry
  val sitemap = List(Menu(Loc("CreateEntry", List("entry"),
			      "Create An Entry",
			      If(User.loggedIn_? _, "Please login"))),
		     Menu(Loc("ViewEntry", List("view"),
			      "View An Entry", Hidden)),
		     Menu(Loc("ViewBlog", List("blog"), "View Blog")))

这里Loc的创建多了几个参数,我们先来看一下Loc的apply函数
 /**
   * Create a Loc (Location) instance
   *
   * @param name -- the name of the location.  This must be unique across your entire sitemap.
   * It's used to look up a menu item in order to create a link to the menu on a page.
   * @param link -- the Link to the page
   * @param text -- the text to display when the link is displayed
   * @param params -- access test, title calculation, etc.
   *
   */
  def apply(theName: String,
            theLink: Link[NullLocParams],
            theText: LinkText[NullLocParams],
            theParams: LocParam*): Loc[NullLocParams]

在前三个基本参数后,还有一个可重复的参数theParams,这个参数用来对Loc进行更细致的控制(参考《The Definitive Guide To Lift》ch5,P65) 。theParams的类型是LocParam(trait net.liftweb.sitemap.Loc.LocParam),这个trait有若干个子类(Test, If, LocInfo, Snippet, LocGroup, LocSnippets, Title, Unless, Template, HttpAuthProtected)这些子类都是case class,通过这些子类的api可以看到每个控制的具体使用方式。我们来看这里用到的几个
i) If
If(User.loggedIn_? _, "Please login")

这是一个If类,If类的定义如下
case class If(val test : () => Boolean, val failMsg : Function0) 
 extends LocParam with Product 

    If the test returns True, the page can be accessed, otherwise, the result of FailMsg will be sent as a response to the browser. If the Loc cannot be accessed, it will not be displayed in menus. 
param
    failMsg - -- what to return the the browser (e.g., 304, etc.) if the page is accessed. 
    test - -- the function that tests access to the page

这里loggedIn_?是在User的基类MetaMegaProtoUser中定义的方法(起这样奇怪的方法名有什么好处我还没体会到), 下划线_是 参数列表的占位符(place holder)。
这个If()的作用是判断用户是否登录,如果不登陆,将向浏览器返回字符串"Please login"。通过这个例子,我们还可以看出sitemap不仅仅是控制菜单项的显示,还控制着页面的访问权限。如果再浏览器直接输入地址/entry来访问的话,将只会得到"Please login"的提示。

ii)Hidden
与其他几个不同Hidden是一个object,他只是一个标志,用来表示这个菜单不会显示。他存在的目的是为了让这个页面可以被访问到,因为缺省情况下,lift是不允许访问在menu中匹配不到的页面的。
  /**
   * If this parameter is included, the item will not be visible in the menu, but
   * will still be accessable.
   */
  case object Hidden extends LocParam

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics