Permalink
Browse files

Moved egg.View into a class, not a plugin

  • Loading branch information...
1 parent d3e2bb5 commit db674fc6b54027d6f6ab4f82bc6e656e74499298 Mark Evans committed Mar 20, 2012
Showing with 64 additions and 61 deletions.
  1. +0 −2 src/js_model_view.coffee
  2. +64 −59 src/view.coffee
View
@@ -1,7 +1,5 @@
egg.jsModelView = (klass)->
- klass.use egg.view
-
klass.include
subscribeToObj: ->
View
@@ -1,80 +1,85 @@
-egg.view = (klass)->
-
- klass.extend
- onDOM: (selector, domEvent, eventName, paramsFunc)->
- @delegatedEvents()["#{domEvent}-#{selector}"] =
- domEvent: domEvent
- selector: selector
- eventName: eventName
- paramsFunc: paramsFunc
+class egg.View extends egg.Base
- delegatedEvents: ->
- @_delegatedEvents ?= {}
-
- onModel: (eventName, callback)->
- @modelSubscriptionSpecs()[eventName] =
- eventName: eventName
- callback: callback
+ # Class methods
- modelSubscriptionSpecs: ->
- @_modelSubscriptionSpecs ?= {}
+ @onDOM: (selector, domEvent, eventName, paramsFunc)->
+ @delegatedEvents()["#{domEvent}-#{selector}"] =
+ domEvent: domEvent
+ selector: selector
+ eventName: eventName
+ paramsFunc: paramsFunc
+
+ @delegatedEvents: ->
+ @_delegatedEvents ?= {}
+
+ @onModel: (eventName, callback)->
+ @modelSubscriptionSpecs()[eventName] =
+ eventName: eventName
+ callback: callback
- klass.init (opts)->
+ @modelSubscriptionSpecs: ->
+ @_modelSubscriptionSpecs ?= {}
+
+ # init and destroy
+
+ constructor: (opts)->
@elem = if opts.elem then $(opts.elem)[0] else throw("Missing elem!")
@model = opts.model
@delegateEvents()
@subscribeToModel() if @model
@setClassName()
+ super
- klass.destroy (opts)->
+ destroy: (opts)->
@unsetClassName()
@unsubscribeToModel() if @model
@undelegateEvents()
+ super
- klass.include
-
- $: (selector)->
- $(@elem).find(selector)
+ # Instance methods
+
+ $: (selector)->
+ $(@elem).find(selector)
- destroyWithElem: ->
- @destroy()
- $(@elem).remove()
+ destroyWithElem: ->
+ @destroy()
+ $(@elem).remove()
- delegateEvents: ->
- @delegatedEventsEnabled = true
- for key, d of @constructor.delegatedEvents()
- $(@elem).on d.domEvent, d.selector, d, (e) =>
- if @delegatedEventsEnabled
- params = {model: @model}
- Object.extend(params, e.data.paramsFunc.call(@, e)) if e.data.paramsFunc
- @emit(e.data.eventName, params)
- e.stopPropagation()
- e.preventDefault()
+ delegateEvents: ->
+ @delegatedEventsEnabled = true
+ for key, d of @constructor.delegatedEvents()
+ $(@elem).on d.domEvent, d.selector, d, (e) =>
+ if @delegatedEventsEnabled
+ params = {model: @model}
+ Object.extend(params, e.data.paramsFunc.call(@, e)) if e.data.paramsFunc
+ @emit(e.data.eventName, params)
+ e.stopPropagation()
+ e.preventDefault()
- undelegateEvents: ->
- @delegatedEventsEnabled = false
+ undelegateEvents: ->
+ @delegatedEventsEnabled = false
- subscribeToModel: ->
- for key, s of @constructor.modelSubscriptionSpecs()
- f = ->
- cb = s.callback
- callback = if typeof cb == 'string'
- (args...) => @[cb](args...)
- else
- (args...) => cb.apply(@, args)
- @modelSubscriptions().push @model.on(s.eventName, callback)
- f.call(@)
+ subscribeToModel: ->
+ for key, s of @constructor.modelSubscriptionSpecs()
+ f = ->
+ cb = s.callback
+ callback = if typeof cb == 'string'
+ (args...) => @[cb](args...)
+ else
+ (args...) => cb.apply(@, args)
+ @modelSubscriptions().push @model.on(s.eventName, callback)
+ f.call(@)
- unsubscribeToModel: ->
- sub.cancel() for sub in @modelSubscriptions()
+ unsubscribeToModel: ->
+ sub.cancel() for sub in @modelSubscriptions()
- modelSubscriptions: ->
- @_modelSubscriptions ?= []
+ modelSubscriptions: ->
+ @_modelSubscriptions ?= []
- setClassName: ->
- if @constructor.className
- $(@elem).addClass(@constructor.className)
+ setClassName: ->
+ if @constructor.className
+ $(@elem).addClass(@constructor.className)
- unsetClassName: ->
- if @constructor.className
- $(@elem).removeClass(@constructor.className)
+ unsetClassName: ->
+ if @constructor.className
+ $(@elem).removeClass(@constructor.className)

0 comments on commit db674fc

Please sign in to comment.