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
61c73853
authored
2012-07-08 23:26:01 -0700
by
Michael Richards
Browse Files
Options
Browse Files
Tag
Download
Plain Diff
Merge branch 'state-binding-refactor'
2 parents
c60e14e6
57c11896
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
76 deletions
lib/rivets.js
src/rivets.coffee
lib/rivets.js
View file @
61c7385
// Generated by CoffeeScript 1.3.3
(
function
()
{
var
Rivets
,
attributeBinding
,
bidirectionals
,
getInputValue
,
rivets
,
stateBinding
,
var
Rivets
,
attributeBinding
,
bidirectionals
,
getInputValue
,
rivets
,
__bind
=
function
(
fn
,
me
){
return
function
(){
return
fn
.
apply
(
me
,
arguments
);
};
},
__indexOf
=
[].
indexOf
||
function
(
item
)
{
for
(
var
i
=
0
,
l
=
this
.
length
;
i
<
l
;
i
++
)
{
if
(
i
in
this
&&
this
[
i
]
===
item
)
return
i
;
}
return
-
1
;
};
...
...
@@ -151,59 +151,53 @@
attributeBinding
=
function
(
attr
)
{
return
function
(
el
,
value
)
{
switch
(
attr
)
{
case
'checked'
:
el
.
setAttribute
(
'checked'
,
value
);
el
.
checked
=
value
;
return
el
.
defaultChecked
=
value
;
default
:
if
(
value
)
{
return
el
.
setAttribute
(
attr
,
value
);
}
else
{
return
el
.
removeAttribute
(
attr
);
}
if
(
value
)
{
return
el
.
setAttribute
(
attr
,
value
);
}
else
{
return
el
.
removeAttribute
(
attr
);
}
};
};
stateBinding
=
function
(
attr
,
inverse
)
{
if
(
inverse
==
null
)
{
inverse
=
false
;
}
return
function
(
el
,
value
)
{
var
binding
;
binding
=
attributeBinding
(
attr
);
return
binding
(
el
,
inverse
===
!
value
?
attr
:
false
);
};
};
bidirectionals
=
[
'value'
,
'checked'
,
'unchecked'
,
'selected'
,
'unselected'
];
Rivets
.
routines
=
{
checked
:
stateBinding
(
'checked'
),
selected
:
stateBinding
(
'selected'
),
disabled
:
stateBinding
(
'disabled'
),
unchecked
:
stateBinding
(
'checked'
,
true
),
unselected
:
stateBinding
(
'selected'
,
true
),
enabled
:
stateBinding
(
'disabled'
,
true
),
text
:
function
(
el
,
value
)
{
if
(
el
.
innerText
!=
null
)
{
return
el
.
innerText
=
value
||
''
;
}
else
{
return
el
.
textContent
=
value
||
''
;
}
enabled
:
function
(
el
,
value
)
{
return
el
.
disabled
=
!
value
;
},
html
:
function
(
el
,
value
)
{
return
el
.
innerHTML
=
value
||
''
;
disabled
:
function
(
el
,
value
)
{
return
el
.
disabled
=
!!
value
;
},
value
:
function
(
el
,
value
)
{
return
el
.
value
=
value
;
checked
:
function
(
el
,
value
)
{
return
el
.
checked
=
!!
value
;
},
unchecked
:
function
(
el
,
value
)
{
return
el
.
checked
=
!
value
;
},
selected
:
function
(
el
,
value
)
{
return
el
.
selected
=
!!
value
;
},
unselected
:
function
(
el
,
value
)
{
return
el
.
selected
=
!
value
;
},
show
:
function
(
el
,
value
)
{
return
el
.
style
.
display
=
value
?
''
:
'none'
;
},
hide
:
function
(
el
,
value
)
{
return
el
.
style
.
display
=
value
?
'none'
:
''
;
},
html
:
function
(
el
,
value
)
{
return
el
.
innerHTML
=
value
||
''
;
},
value
:
function
(
el
,
value
)
{
return
el
.
value
=
value
;
},
text
:
function
(
el
,
value
)
{
if
(
el
.
innerText
!=
null
)
{
return
el
.
innerText
=
value
||
''
;
}
else
{
return
el
.
textContent
=
value
||
''
;
}
}
};
...
...
src/rivets.coffee
View file @
61c7385
...
...
@@ -86,23 +86,7 @@ getInputValue = (el) ->
# 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
)
->
switch
attr
when
'checked'
el
.
setAttribute
(
'checked'
,
value
)
el
.
checked
=
value
el
.
defaultChecked
=
value
else
if
value
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
)
->
binding
=
attributeBinding
(
attr
)
binding
el
,
if
inverse
is
!
value
then
attr
else
false
if
value
then
el
.
setAttribute
attr
,
value
else
el
.
removeAttribute
attr
# Bindings that should also be observed for changes on the DOM element in order
# to propagate those changes back to the model object.
...
...
@@ -110,31 +94,31 @@ bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected']
# Core binding routines.
Rivets
.
routines
=
checked
:
stateBinding
'checked'
selected
:
stateBinding
'selected'
disabled
:
stateBinding
'disabled'
unchecked
:
stateBinding
'checked'
,
true
unselected
:
stateBinding
'selected'
,
true
enabled
:
stateBinding
'disabled'
,
true
enabled
:
(
el
,
value
)
->
el
.
disabled
=
!
value
disabled
:
(
el
,
value
)
->
el
.
disabled
=
!!
value
checked
:
(
el
,
value
)
->
el
.
checked
=
!!
value
unchecked
:
(
el
,
value
)
->
el
.
checked
=
!
value
selected
:
(
el
,
value
)
->
el
.
selected
=
!!
value
unselected
:
(
el
,
value
)
->
el
.
selected
=
!
value
show
:
(
el
,
value
)
->
el
.
style
.
display
=
if
value
then
''
else
'none'
hide
:
(
el
,
value
)
->
el
.
style
.
display
=
if
value
then
'none'
else
''
html
:
(
el
,
value
)
->
el
.
innerHTML
=
value
or
''
value
:
(
el
,
value
)
->
el
.
value
=
value
text
:
(
el
,
value
)
->
if
el
.
innerText
?
el
.
innerText
=
value
or
''
else
el
.
textContent
=
value
or
''
html
:
(
el
,
value
)
->
el
.
innerHTML
=
value
or
''
value
:
(
el
,
value
)
->
el
.
value
=
value
show
:
(
el
,
value
)
->
el
.
style
.
display
=
if
value
then
''
else
'none'
hide
:
(
el
,
value
)
->
el
.
style
.
display
=
if
value
then
'none'
else
''
# Default configuration.
Rivets
.
config
=
...
...
Please
register
or
sign in
to post a comment