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
f1d4a2c9
authored
2012-05-06 02:00:49 -0700
by
Michael Richards
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Implement wildcard attribute bindings so that it's possible to bind to any arbitrary attribute.
1 parent
d1af20dd
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
19 additions
and
27 deletions
README.md
lib/rivets.js
src/rivets.coffee
README.md
View file @
f1d4a2c
...
...
@@ -24,7 +24,7 @@ No contrived example here yet, but the `rivets` module is simple. It exposes a s
-
**data-unchecked**
: two-way inverse binding that sets the node's checked state.
-
**data-selected**
: two-way binding that sets the node's selected state.
-
**data-unselected**
: two-way inverse binding that sets the node's checked state.
-
**data-[attribute]**
: one-way binding that sets the node's attribute value
(currently only for a few select attributes)
.
-
**data-[attribute]**
: one-way binding that sets the node's attribute value.
## Adapters
...
...
lib/rivets.js
View file @
f1d4a2c
...
...
@@ -3,11 +3,13 @@
var
__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
;
};
window
.
rivets
=
(
function
()
{
var
attr
,
bidirectionalBindings
,
bindableAttributes
,
bindings
,
getInputValue
,
registerBinding
,
setAttribute
,
_fn
,
_i
,
_len
;
var
attr
ibuteBinding
,
bidirectionalBindings
,
bindings
,
getInputValue
,
registerBinding
,
setAttribute
;
registerBinding
=
function
(
el
,
adapter
,
type
,
context
,
keypath
)
{
bindings
[
type
](
el
,
adapter
.
read
(
context
,
keypath
));
var
bind
;
bind
=
bindings
[
type
]
||
attributeBinding
(
type
);
bind
(
el
,
adapter
.
read
(
context
,
keypath
));
adapter
.
subscribe
(
context
,
keypath
,
function
(
value
)
{
return
bind
ings
[
type
]
(
el
,
value
);
return
bind
(
el
,
value
);
});
if
(
__indexOf
.
call
(
bidirectionalBindings
,
type
)
>=
0
)
{
return
$
(
el
).
bind
(
'change'
,
function
()
{
...
...
@@ -76,43 +78,36 @@
return
$
(
el
).
val
(
value
);
}
};
bidirectionalBindings
=
[
'value'
,
'checked'
,
'unchecked'
,
'selected'
,
'unselected'
];
bindableAttributes
=
[
'id'
,
'class'
,
'name'
,
'src'
,
'href'
,
'alt'
,
'title'
,
'placeholder'
];
_fn
=
function
(
attr
)
{
return
bindings
[
attr
]
=
function
(
el
,
value
)
{
attributeBinding
=
function
(
attr
)
{
return
function
(
el
,
value
)
{
return
setAttribute
(
el
,
attr
,
value
);
};
};
for
(
_i
=
0
,
_len
=
bindableAttributes
.
length
;
_i
<
_len
;
_i
++
)
{
attr
=
bindableAttributes
[
_i
];
_fn
(
attr
);
}
bidirectionalBindings
=
[
'value'
,
'checked'
,
'unchecked'
,
'selected'
,
'unselected'
];
return
{
bind
:
function
(
el
,
adapter
,
contexts
)
{
if
(
contexts
==
null
)
{
contexts
=
{};
}
return
$
(
el
).
add
(
$
(
'*'
,
el
)).
each
(
function
()
{
var
nodeMap
,
target
,
_
j
,
_ref
,
_results
;
var
nodeMap
,
target
,
_
i
,
_ref
,
_results
;
target
=
this
;
nodeMap
=
target
.
attributes
;
if
(
nodeMap
.
length
>
0
)
{
return
(
function
()
{
_results
=
[];
for
(
var
_
j
=
0
,
_ref
=
nodeMap
.
length
-
1
;
0
<=
_ref
?
_j
<=
_ref
:
_j
>=
_ref
;
0
<=
_ref
?
_j
++
:
_j
--
){
_results
.
push
(
_j
);
}
for
(
var
_
i
=
0
,
_ref
=
nodeMap
.
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
(
node
.
name
))
{
type
=
node
.
name
.
replace
(
'data-'
,
''
);
if
(
type
in
bindings
)
{
path
=
node
.
value
.
split
(
'.'
);
context
=
path
.
shift
();
keypath
=
path
.
join
(
'.'
);
return
registerBinding
(
$
(
target
),
adapter
,
type
,
contexts
[
context
],
keypath
);
}
}
});
}
});
...
...
src/rivets.coffee
View file @
f1d4a2c
...
...
@@ -5,14 +5,15 @@
window
.
rivets
=
do
->
registerBinding
=
(
el
,
adapter
,
type
,
context
,
keypath
)
->
bindings
[
type
]
el
,
adapter
.
read
(
context
,
keypath
)
bind
=
bindings
[
type
]
||
attributeBinding
type
bind
el
,
adapter
.
read
context
,
keypath
adapter
.
subscribe
context
,
keypath
,
(
value
)
->
bind
ings
[
type
]
el
,
value
bind
el
,
value
if
type
in
bidirectionalBindings
$
(
el
).
bind
'change'
,
->
adapter
.
publish
context
,
keypath
,
getInputValue
(
this
)
adapter
.
publish
context
,
keypath
,
getInputValue
this
setAttribute
=
(
el
,
attr
,
value
,
mirrored
=
false
)
->
if
value
...
...
@@ -47,14 +48,12 @@ window.rivets = do ->
value
:
(
el
,
value
)
->
$
(
el
).
val
value
bidirectionalBindings
=
[
'value'
,
'checked'
,
'unchecked'
,
'selected'
,
'unselected'
]
bindableAttributes
=
[
'id'
,
'class'
,
'name'
,
'src'
,
'href'
,
'alt'
,
'title'
,
'placeholder'
]
for
attr
in
bindableAttributes
do
(
attr
)
->
bindings
[
attr
]
=
(
el
,
value
)
->
attributeBinding
=
(
attr
)
->
(
el
,
value
)
->
setAttribute
el
,
attr
,
value
bidirectionalBindings
=
[
'value'
,
'checked'
,
'unchecked'
,
'selected'
,
'unselected'
]
bind
:
(
el
,
adapter
,
contexts
=
{})
->
$
(
el
).
add
(
$
(
'*'
,
el
)).
each
->
target
=
this
...
...
@@ -66,8 +65,6 @@ window.rivets = do ->
if
/^data-/
.
test
node
.
name
type
=
node
.
name
.
replace
'data-'
,
''
if
type
of
bindings
path
=
node
.
value
.
split
'.'
context
=
path
.
shift
()
keypath
=
path
.
join
'.'
...
...
Please
register
or
sign in
to post a comment