Skip to content

Commit

Permalink
remove auto digest from DOM events, sometimes we need to use $.prop
Browse files Browse the repository at this point in the history
  • Loading branch information
andreypopp committed Mar 13, 2013
1 parent b22a978 commit f0bf386
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 82 deletions.
43 changes: 16 additions & 27 deletions backbone.viewdsl.coffee
Expand Up @@ -21,7 +21,7 @@


) this, (_, Backbone, require) -> ) this, (_, Backbone, require) ->


{some, every, extend, toArray, isEqual, isBoolean, isString} = _ {some, every, extend, toArray, isEqual, isBoolean, isString, contains} = _


resolvePath = (o, p) -> resolvePath = (o, p) ->
p = p.trim() p = p.trim()
Expand Down Expand Up @@ -196,6 +196,8 @@


$node $node


domProperties = ['value', 'checked', 'disabled']

Directives = Directives =


compileInterpolation: ($node, value) -> compileInterpolation: ($node, value) ->
Expand Down Expand Up @@ -225,17 +227,21 @@
observe = true observe = true
attrName = name.substring(5) attrName = name.substring(5)
$node.removeAttr(name) $node.removeAttr(name)
{attr, removeAttr} = if contains(domProperties, attrName)
{attr: 'prop', removeAttr: 'removeProp'}
else
{attr: 'attr', removeAttr: 'removeAttr'}
(scope, $node) -> (scope, $node) ->
scope.reactOn value, scope.reactOn value,
observe: observe observe: observe
react: (got) -> react: (got) ->
if isBoolean(got) if isBoolean(got)
if got if got
$node.attr(attrName, '') $node[attr](attrName, '')
else else
$node.removeAttr(attrName) $node[removeAttr](attrName)
else else
$node.attr(attrName, got) $node[attr](attrName, got)


compileClass: ($node, name, value) -> compileClass: ($node, name, value) ->
observe = false observe = false
Expand Down Expand Up @@ -330,21 +336,22 @@
template: undefined template: undefined


constructor: (options = {}) -> constructor: (options = {}) ->
super
this.template = options.template if options.template? this.template = options.template if options.template?
this.parent = options.parent this.parent = options.parent
this.views = [] this.views = []
this.compiler = new Compiler(this.constructor) this.compiler = new Compiler(this.constructor)


if this.model? if options.model?
this.listenTo this.model, 'change' this.listenTo options.model, 'change'


if this.collection? if options.collection?
this.listenTo this.collection, 'change add remove reset sort' this.listenTo options.collection, 'change add remove reset sort'


this.digestScheduled = false this.digestScheduled = false
this.observe = {} this.observe = {}


super

renderTemplate: (template) -> renderTemplate: (template) ->
if not (template instanceof Template) if not (template instanceof Template)
template = this.compiler.compile($parseHTML template) template = this.compiler.compile($parseHTML template)
Expand Down Expand Up @@ -401,24 +408,6 @@
if options.observe if options.observe
this.listenTo this, "change:#{p}", options.react this.listenTo this, "change:#{p}", options.react


delegateEvents: (events) ->
if not (events or (events = _.result(this, 'events')))
return this
this.undelegateEvents()
for key, method of events
method = this[events[key]] unless _.isFunction(method)
throw new Error("Method \"#{events[key]}\" does not exist") unless method
match = key.match(delegateEventSplitter)
eventName = match[1]
selector = match[2]
method = @mutating _.bind(method, this)
eventName += '.delegateEvents' + this.cid
if selector == ''
this.$el.on(eventName, method)
else
this.$el.on(eventName, selector, method)
this

listenTo: (obj, name, cb) -> listenTo: (obj, name, cb) ->
if typeof name == 'object' if typeof name == 'object'
for k, v of name for k, v of name
Expand Down
58 changes: 19 additions & 39 deletions backbone.viewdsl.js

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

11 changes: 1 addition & 10 deletions tests/lib/tests.js

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

6 changes: 0 additions & 6 deletions tests/tests.coffee
Expand Up @@ -116,12 +116,6 @@ define (require) ->
et.trigger 'event' et.trigger 'event'
expect(v.$el.html()).to.be.equal '<div>Hello, Andrey!</div>' expect(v.$el.html()).to.be.equal '<div>Hello, Andrey!</div>'


it 'should digest on DOM event handling', ->
v = renderV '<div>Hello, {{bind:name}}!</div>', {name: 'World'}
expect(v.$el.html()).to.be.equal '<div>Hello, World!</div>'
v.$el.trigger('event')
expect(v.$el.html()).to.be.equal '<div>Hello, Andrey!</div>'

describe 'View', -> describe 'View', ->


render = (t, s) -> render = (t, s) ->
Expand Down

0 comments on commit f0bf386

Please sign in to comment.