Skip to content

Instantly share code, notes, and snippets.

@yusugomori
Created March 30, 2012 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusugomori/2251506 to your computer and use it in GitHub Desktop.
Save yusugomori/2251506 to your computer and use it in GitHub Desktop.
Tower.js toggle @layout
#
# applicationController.coffee
#
class App.ApplicationController extends Tower.Controller
# @layout "application" # ここに@layoutを置くと、上書きできない。
constructor: ->
@layout = "application" # @layoutはcontructorの中に。
@userAgent = ""
# renderメソッドをオーバーライドする。
render: (path, options={})->
if typeof path isnt 'string' # e.g. @render text: "This is text", status: 200
options = path
path = ""
@setUserAgent() # User-Agentをセット。
if @userAgent.match /mobile/
@layout = "application-mobile"
path = "mobile/#{path}"
unless options.layout? # @render "welcome/index", layout: false などに対応。
options.layout = @layout
super path, options
setUserAgent: () ->
@userAgent = @getUserAgent().join(" ")
getUserAgent: () ->
arr = []
ua = @request.headers['user-agent'].toLowerCase()
# iPhone
if ua.match /iphone/
arr.push "iphone", "mobile"
# Androidは省略
return arr
#
# welcomesController.coffee (Just an example)
#
class App.WelcomeController extends App.ApplicationController
index: ->
@render text: "This is welcome#index"
about: ->
@render "welcome/about", layout: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment