Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
brainfood
/
rivets
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
3fe45ff0
authored
2012-05-15 00:01:32 -0700
by
Michael Richards
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Add comments to source.
1 parent
fe9918e1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
4 deletions
src/rivets.coffee
src/rivets.coffee
View file @
3fe45ff
# rivets.js
# version : 0.1.2
# author : Michael Richards
# license : MIT
#
rivets.js
#
version : 0.1.2
#
author : Michael Richards
#
license : MIT
# Registers a specific binding routine between a model object and a DOM element.
# All information for that routine is passed in when calling this function; the
# specific element to bind to, an adapter interface for the model object, the
# type of binding, the context object and the keypath at which to subscribe to
# on the model object.
registerBinding
=
(
el
,
adapter
,
type
,
context
,
keypath
)
->
bind
=
bindings
[
type
]
||
attributeBinding
type
bind
el
,
adapter
.
read
context
,
keypath
...
...
@@ -14,17 +19,24 @@ registerBinding = (el, adapter, type, context, keypath) ->
el
.
addEventListener
'change'
,
->
adapter
.
publish
context
,
keypath
,
getInputValue
this
# Returns the current input value for the specified element.
getInputValue
=
(
el
)
->
switch
el
.
type
when
'text'
,
'textarea'
,
'password'
,
'select-one'
then
el
.
value
when
'checkbox'
,
'radio'
then
el
.
checked
# Returns an attribute binding routine for the specified attribute. This is what
# `registerBinding` falls back to when there is no routine for the binding type.
attributeBinding
=
(
attr
)
->
(
el
,
value
)
->
if
value
then
el
.
setAttribute
attr
,
value
else
el
.
removeAttribute
attr
# Returns a state binding routine for the specified attribute. Can optionally be
# negatively evaluated. This is used to build a lot of the core state binding
# routines.
stateBinding
=
(
attr
,
inverse
=
false
)
->
(
el
,
value
)
->
attributeBinding
(
attr
)
el
,
if
inverse
is
!
value
then
attr
else
false
# Core binding routines.
bindings
=
checked
:
stateBinding
'checked'
...
...
@@ -49,8 +61,11 @@ bindings =
hide
:
(
el
,
value
)
->
el
.
style
.
display
=
if
value
then
'none'
else
''
# Bindings that should also be observed for changes on the DOM element in order
# to propogate those changes back to the model object.
bidirectionals
=
[
'value'
,
'checked'
,
'unchecked'
,
'selected'
,
'unselected'
]
# The rivets module. Exposes a single `bind` function.
rivets
=
bind
:
(
el
,
adapter
,
contexts
=
{})
->
for
node
in
el
.
getElementsByTagName
'*'
...
...
@@ -62,6 +77,7 @@ rivets =
keypath
=
path
.
join
'.'
registerBinding
node
,
adapter
,
type
,
contexts
[
context
],
keypath
# Exports rivets for both CommonJS and the browser.
if
module
?
module
.
exports
=
rivets
else
...
...
Please
register
or
sign in
to post a comment