Skip to content

Commit

Permalink
Fix the issue of group view
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyuntao committed Jul 1, 2013
1 parent 0ecbb6e commit d069625
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 36 deletions.
34 changes: 16 additions & 18 deletions lib/js/kopi/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lib/js/kopi/utils/klass.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 19 additions & 18 deletions src/coffee/kopi/app.coffee
Expand Up @@ -18,7 +18,6 @@ define "kopi/app", (require, exports, module) ->
loc = location
baseURL = uri.current()
logger = logging.logger(module.id)
appInstance = null

###
Application class
Expand Down Expand Up @@ -55,15 +54,14 @@ define "kopi/app", (require, exports, module) ->
@LOCK_EVENT = "lock"
@UNLOCK_EVENT = "unlock"

klass.singleton this

klass.configure this

klass.accessor @prototype, "router"

constructor: (options={}) ->
# Make sure only one app is launched in current page
throw new Error("Only one app can be initialized in current page") if appInstance
# Set singleton of application
appInstance = this
@_isSingleton()

self = this
self.guid = utils.guid("app")
Expand Down Expand Up @@ -107,15 +105,15 @@ define "kopi/app", (require, exports, module) ->
cls = this.constructor
self = this
if self.started
logger.warn("App has already been launched.")
logger.warn("[app:start] App has already been launched.")
return self
# Ensure layout elements
self.container = $("body")
self.viewport = viewport.instance()
self.viewport.skeleton().render()
self._listenToURLChange()
self.emit(cls.START_EVENT)
logger.info "Start app: #{self.guid}"
logger.info "[app:start] Start app: #{self.guid}"
self.started = true
# Load current URL
unless support.history and self._options.usePushState
Expand All @@ -126,7 +124,6 @@ define "kopi/app", (require, exports, module) ->
self = this
self._stopListenToURLChange()
self.started = false
appInstance = null
self

getCurrentURL: ->
Expand All @@ -140,7 +137,7 @@ define "kopi/app", (require, exports, module) ->
@param {Hash} options
###
load: (url, options) ->
logger.info "Load URL: #{url}"
logger.info "[app:load] Load URL: #{url}"
cls = this.constructor
self = this
url = uri.parse uri.absolute(url)
Expand Down Expand Up @@ -169,16 +166,16 @@ define "kopi/app", (require, exports, module) ->
@param {kopi.utils.uri.URI} url
###
onrequest: (e, url, options) ->
logger.info "Receive request: #{url.path}"
logger.info "[app:onrequest] Receive request: #{url.path}"
self = this
cls = this.constructor
match = self._match(url)

if not match
logger.info "No matching view found."
logger.info "[app:onrequest] No matching view found."
if self._options.redirectWhenNoRouteFound
url = uri.unparse url
logger.info("Redirect to URL: #{url}")
logger.info("[app:onrequest] Redirect to URL: #{url}")
uri.goto url
return

Expand Down Expand Up @@ -224,7 +221,7 @@ define "kopi/app", (require, exports, module) ->
self._useHash = true
self._interval = setInterval checkFn, self._options.interval
else
logger.warn("App will not repond to url change")
logger.warn("[app:_listenToURLChange] App will not repond to url change")
return

###
Expand Down Expand Up @@ -269,20 +266,21 @@ define "kopi/app", (require, exports, module) ->
@return {kopi.views.View}
###
_match: (url) ->
return logger.warn "Router is not provided" unless @_router
return logger.warn "[app:_match] Router is not provided" unless @_router

self = this
path = uri.parse(url).path
request = @_router.match(path)

# If no proper router is found
return logger.warn("Can not find proper route for path: #{path}") unless request
return logger.warn("[app:_match] Can not find proper route for path: #{path}") unless request

route = request.route
for guid, view of self._views
# If `group` is `true`, use same view for every URL matches route
if route.group is true and view.name == route.view.name
return [view, request]
if route.group is true and view.constructor.viewName() == route.view.viewName()
logger.log "[app:_match] group: true"
return [view, request]
# If `group` is `string`, use same view for every URL matches route
else if text.isString(route.group)
if view.params[route.group] == route.params[route.group]
Expand All @@ -307,4 +305,7 @@ define "kopi/app", (require, exports, module) ->
[view, request]

App: App
instance: -> appInstance
# DEPRECATED
# Use App.instance() instead
# -- Wu Yuntao, 2013-07-01
instance: -> App.instance()
4 changes: 4 additions & 0 deletions src/coffee/kopi/utils/klass.coffee
Expand Up @@ -112,8 +112,12 @@ define "kopi/utils/klass", (require, exports, module) ->
###
singleton = (klass) ->
instance = null
# Class method to get singleton
klass.instance = -> instance
# Class method to remove singleton
klass.removeInstance = -> instance = null
# A private instance method to check if instance
# can be initialized as a singleton
klass.prototype._isSingleton = ->
throw new SingletonError(klass) if instance
instance = this
Expand Down

0 comments on commit d069625

Please sign in to comment.