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
4f30463c
authored
2012-05-10 00:02:51 -0700
by
Michael Richards
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Remove jQuery as a dependency.
1 parent
3f499b5a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
50 deletions
lib/rivets.js
src/rivets.coffee
lib/rivets.js
View file @
4f30463
...
...
@@ -12,7 +12,7 @@
return
bind
(
el
,
value
);
});
if
(
__indexOf
.
call
(
bidirectionalBindings
,
type
)
>=
0
)
{
return
$
(
el
).
bind
(
'change'
,
function
()
{
return
el
.
addEventListener
(
'change'
,
function
()
{
return
adapter
.
publish
(
context
,
keypath
,
getInputValue
(
this
));
});
}
...
...
@@ -22,36 +22,28 @@
mirrored
=
false
;
}
if
(
value
)
{
return
$
(
el
).
attr
(
attr
,
mirrored
?
attr
:
value
);
return
el
.
setAttribute
(
attr
,
mirrored
?
attr
:
value
);
}
else
{
return
$
(
el
).
removeAttr
(
attr
);
return
el
.
removeAttribute
(
attr
);
}
};
getInputValue
=
function
(
el
)
{
switch
(
$
(
el
).
attr
(
'type'
)
)
{
switch
(
el
.
type
)
{
case
'text'
:
case
'textarea'
:
case
'password'
:
case
'select-one'
:
return
$
(
el
).
val
()
;
return
el
.
value
;
case
'checkbox'
:
return
$
(
el
).
is
(
':checked'
)
;
return
el
.
checked
;
}
};
bindings
=
{
show
:
function
(
el
,
value
)
{
if
(
value
)
{
return
$
(
el
).
show
();
}
else
{
return
$
(
el
).
hide
();
}
return
el
.
style
.
display
=
value
?
''
:
'none'
;
},
hide
:
function
(
el
,
value
)
{
if
(
value
)
{
return
$
(
el
).
hide
();
}
else
{
return
$
(
el
).
show
();
}
return
el
.
style
.
display
=
value
?
'none'
:
''
;
},
enabled
:
function
(
el
,
value
)
{
return
setAttribute
(
el
,
'disabled'
,
!
value
,
true
);
...
...
@@ -72,10 +64,10 @@
return
setAttribute
(
el
,
'checked'
,
!
value
,
true
);
},
text
:
function
(
el
,
value
)
{
return
$
(
el
).
text
(
value
||
''
)
;
return
el
.
innerHTML
=
value
||
''
;
},
value
:
function
(
el
,
value
)
{
return
$
(
el
).
val
(
value
)
;
return
el
.
value
=
value
;
}
};
attributeBinding
=
function
(
attr
)
{
...
...
@@ -86,27 +78,32 @@
bidirectionalBindings
=
[
'value'
,
'checked'
,
'unchecked'
,
'selected'
,
'unselected'
];
return
{
bind
:
function
(
el
,
adapter
,
contexts
)
{
var
nodes
,
_i
,
_ref
,
_results
;
if
(
contexts
==
null
)
{
contexts
=
{};
}
return
$
(
el
).
add
(
$
(
'*'
,
el
)).
each
(
function
()
{
var
nodeMap
,
target
,
_i
,
_ref
,
_results
;
target
=
this
;
nodeMap
=
target
.
attributes
;
if
(
nodeMap
.
length
>
0
)
{
nodes
=
el
.
getElementsByTagName
(
'*'
);
return
(
function
()
{
_results
=
[];
for
(
var
_i
=
0
,
_ref
=
nodes
.
length
-
1
;
0
<=
_ref
?
_i
<=
_ref
:
_i
>=
_ref
;
0
<=
_ref
?
_i
++
:
_i
--
){
_results
.
push
(
_i
);
}
return
_results
;
}).
apply
(
this
).
forEach
(
function
(
n
)
{
var
node
,
_i
,
_ref
,
_results
;
node
=
nodes
[
n
];
if
(
node
.
attributes
.
length
>
0
)
{
return
(
function
()
{
_results
=
[];
for
(
var
_i
=
0
,
_ref
=
node
Map
.
length
-
1
;
0
<=
_ref
?
_i
<=
_ref
:
_i
>=
_ref
;
0
<=
_ref
?
_i
++
:
_i
--
){
_results
.
push
(
_i
);
}
for
(
var
_i
=
0
,
_ref
=
node
.
attributes
.
length
-
1
;
0
<=
_ref
?
_i
<=
_ref
:
_i
>=
_ref
;
0
<=
_ref
?
_i
++
:
_i
--
){
_results
.
push
(
_i
);
}
return
_results
;
}).
apply
(
this
).
forEach
(
function
(
n
)
{
var
context
,
keypath
,
node
,
path
,
type
;
node
=
nodeMap
[
n
];
if
(
/^data-/
.
test
(
nod
e
.
name
))
{
type
=
nod
e
.
name
.
replace
(
'data-'
,
''
);
path
=
nod
e
.
value
.
split
(
'.'
);
var
attribute
,
context
,
keypath
,
path
,
type
;
attribute
=
node
.
attributes
[
n
];
if
(
/^data-/
.
test
(
attribut
e
.
name
))
{
type
=
attribut
e
.
name
.
replace
(
'data-'
,
''
);
path
=
attribut
e
.
value
.
split
(
'.'
);
context
=
path
.
shift
();
keypath
=
path
.
join
(
'.'
);
return
registerBinding
(
$
(
target
)
,
adapter
,
type
,
contexts
[
context
],
keypath
);
return
registerBinding
(
node
,
adapter
,
type
,
contexts
[
context
],
keypath
);
}
});
}
...
...
src/rivets.coffee
View file @
4f30463
...
...
@@ -12,25 +12,25 @@ window.rivets = do ->
bind
el
,
value
if
type
in
bidirectionalBindings
$
(
el
).
bind
'change'
,
->
el
.
addEventListener
'change'
,
->
adapter
.
publish
context
,
keypath
,
getInputValue
this
setAttribute
=
(
el
,
attr
,
value
,
mirrored
=
false
)
->
if
value
$
(
el
).
attr
attr
,
if
mirrored
then
attr
else
value
el
.
setAttribute
attr
,
if
mirrored
then
attr
else
value
else
$
(
el
).
removeAttr
attr
el
.
removeAttribute
attr
getInputValue
=
(
el
)
->
switch
$
(
el
).
attr
'type'
when
'text'
,
'textarea'
,
'password'
,
'select-one'
then
$
(
el
).
val
()
when
'checkbox'
then
$
(
el
).
is
':checked'
switch
el
.
type
when
'text'
,
'textarea'
,
'password'
,
'select-one'
then
el
.
value
when
'checkbox'
then
el
.
checked
bindings
=
show
:
(
el
,
value
)
->
if
value
then
$
(
el
).
show
()
else
$
(
el
).
hide
()
el
.
style
.
display
=
if
value
then
''
else
'none'
hide
:
(
el
,
value
)
->
if
value
then
$
(
el
).
hide
()
else
$
(
el
).
show
()
el
.
style
.
display
=
if
value
then
'none'
else
''
enabled
:
(
el
,
value
)
->
setAttribute
el
,
'disabled'
,
!
value
,
true
disabled
:
(
el
,
value
)
->
...
...
@@ -44,9 +44,9 @@ window.rivets = do ->
unselected
:
(
el
,
value
)
->
setAttribute
el
,
'checked'
,
!
value
,
true
text
:
(
el
,
value
)
->
$
(
el
).
text
value
or
''
el
.
innerHTML
=
value
or
''
value
:
(
el
,
value
)
->
$
(
el
).
val
value
el
.
value
=
value
attributeBinding
=
(
attr
)
->
(
el
,
value
)
->
...
...
@@ -55,17 +55,18 @@ window.rivets = do ->
bidirectionalBindings
=
[
'value'
,
'checked'
,
'unchecked'
,
'selected'
,
'unselected'
]
bind
:
(
el
,
adapter
,
contexts
=
{})
->
$
(
el
).
add
(
$
(
'*'
,
el
)).
each
->
target
=
this
nodeMap
=
target
.
attributes
nodes
=
el
.
getElementsByTagName
'*'
if
nodeMap
.
length
>
0
[
0
..(
nodeMap
.
length
-
1
)].
forEach
(
n
)
->
node
=
nodeMap
[
n
]
[
0
..(
nodes
.
length
-
1
)].
forEach
(
n
)
->
node
=
nodes
[
n
]
if
node
.
attributes
.
length
>
0
[
0
..(
node
.
attributes
.
length
-
1
)].
forEach
(
n
)
->
attribute
=
node
.
attributes
[
n
]
if
/^data-/
.
test
nod
e
.
name
type
=
nod
e
.
name
.
replace
'data-'
,
''
path
=
nod
e
.
value
.
split
'.'
if
/^data-/
.
test
attribut
e
.
name
type
=
attribut
e
.
name
.
replace
'data-'
,
''
path
=
attribut
e
.
value
.
split
'.'
context
=
path
.
shift
()
keypath
=
path
.
join
'.'
registerBinding
$
(
target
)
,
adapter
,
type
,
contexts
[
context
],
keypath
registerBinding
node
,
adapter
,
type
,
contexts
[
context
],
keypath
...
...
Please
register
or
sign in
to post a comment