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
d9e1bb5d
authored
2012-05-12 16:22:52 -0700
by
Michael Richards
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Export library for both CommonJS and the browser.
1 parent
4f30463c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
165 additions
and
150 deletions
lib/rivets.js
src/rivets.coffee
lib/rivets.js
View file @
d9e1bb5
// Generated by CoffeeScript 1.3.1
(
function
()
{
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
;
};
var
attributeBinding
,
bidirectionalBindings
,
bindings
,
getInputValue
,
registerBinding
,
rivets
,
setAttribute
,
__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
attributeBinding
,
bidirectionalBindings
,
bindings
,
getInputValue
,
registerBinding
,
setAttribute
;
registerBinding
=
function
(
el
,
adapter
,
type
,
context
,
keypath
)
{
var
bind
;
bind
=
bindings
[
type
]
||
attributeBinding
(
type
);
bind
(
el
,
adapter
.
read
(
context
,
keypath
));
adapter
.
subscribe
(
context
,
keypath
,
function
(
value
)
{
return
bind
(
el
,
value
);
registerBinding
=
function
(
el
,
adapter
,
type
,
context
,
keypath
)
{
var
bind
;
bind
=
bindings
[
type
]
||
attributeBinding
(
type
);
bind
(
el
,
adapter
.
read
(
context
,
keypath
));
adapter
.
subscribe
(
context
,
keypath
,
function
(
value
)
{
return
bind
(
el
,
value
);
});
if
(
__indexOf
.
call
(
bidirectionalBindings
,
type
)
>=
0
)
{
return
el
.
addEventListener
(
'change'
,
function
()
{
return
adapter
.
publish
(
context
,
keypath
,
getInputValue
(
this
));
});
if
(
__indexOf
.
call
(
bidirectionalBindings
,
type
)
>=
0
)
{
return
el
.
addEventListener
(
'change'
,
function
()
{
return
adapter
.
publish
(
context
,
keypath
,
getInputValue
(
this
));
});
}
};
setAttribute
=
function
(
el
,
attr
,
value
,
mirrored
)
{
if
(
mirrored
==
null
)
{
mirrored
=
false
;
}
if
(
value
)
{
return
el
.
setAttribute
(
attr
,
mirrored
?
attr
:
value
);
}
else
{
return
el
.
removeAttribute
(
attr
);
}
};
getInputValue
=
function
(
el
)
{
switch
(
el
.
type
)
{
case
'text'
:
case
'textarea'
:
case
'password'
:
case
'select-one'
:
return
el
.
value
;
case
'checkbox'
:
return
el
.
checked
;
}
}
};
setAttribute
=
function
(
el
,
attr
,
value
,
mirrored
)
{
if
(
mirrored
==
null
)
{
mirrored
=
false
;
}
if
(
value
)
{
return
el
.
setAttribute
(
attr
,
mirrored
?
attr
:
value
);
}
else
{
return
el
.
removeAttribute
(
attr
);
}
};
getInputValue
=
function
(
el
)
{
switch
(
el
.
type
)
{
case
'text'
:
case
'textarea'
:
case
'password'
:
case
'select-one'
:
return
el
.
value
;
case
'checkbox'
:
return
el
.
checked
;
}
};
bindings
=
{
show
:
function
(
el
,
value
)
{
return
el
.
style
.
display
=
value
?
''
:
'none'
;
},
hide
:
function
(
el
,
value
)
{
return
el
.
style
.
display
=
value
?
'none'
:
''
;
},
enabled
:
function
(
el
,
value
)
{
return
setAttribute
(
el
,
'disabled'
,
!
value
,
true
);
},
disabled
:
function
(
el
,
value
)
{
return
setAttribute
(
el
,
'disabled'
,
value
,
true
);
},
checked
:
function
(
el
,
value
)
{
return
setAttribute
(
el
,
'checked'
,
value
,
true
);
},
unchecked
:
function
(
el
,
value
)
{
return
setAttribute
(
el
,
'checked'
,
!
value
,
true
);
},
selected
:
function
(
el
,
value
)
{
return
setAttribute
(
el
,
'selected'
,
value
,
true
);
},
unselected
:
function
(
el
,
value
)
{
return
setAttribute
(
el
,
'checked'
,
!
value
,
true
);
},
text
:
function
(
el
,
value
)
{
return
el
.
innerHTML
=
value
||
''
;
},
value
:
function
(
el
,
value
)
{
return
el
.
value
=
value
;
}
};
attributeBinding
=
function
(
attr
)
{
return
function
(
el
,
value
)
{
return
setAttribute
(
el
,
attr
,
value
);
};
bindings
=
{
show
:
function
(
el
,
value
)
{
return
el
.
style
.
display
=
value
?
''
:
'none'
;
},
hide
:
function
(
el
,
value
)
{
return
el
.
style
.
display
=
value
?
'none'
:
''
;
},
enabled
:
function
(
el
,
value
)
{
return
setAttribute
(
el
,
'disabled'
,
!
value
,
true
);
},
disabled
:
function
(
el
,
value
)
{
return
setAttribute
(
el
,
'disabled'
,
value
,
true
);
},
checked
:
function
(
el
,
value
)
{
return
setAttribute
(
el
,
'checked'
,
value
,
true
);
},
unchecked
:
function
(
el
,
value
)
{
return
setAttribute
(
el
,
'checked'
,
!
value
,
true
);
},
selected
:
function
(
el
,
value
)
{
return
setAttribute
(
el
,
'selected'
,
value
,
true
);
},
unselected
:
function
(
el
,
value
)
{
return
setAttribute
(
el
,
'checked'
,
!
value
,
true
);
},
text
:
function
(
el
,
value
)
{
return
el
.
innerHTML
=
value
||
''
;
},
value
:
function
(
el
,
value
)
{
return
el
.
value
=
value
;
};
bidirectionalBindings
=
[
'value'
,
'checked'
,
'unchecked'
,
'selected'
,
'unselected'
];
rivets
=
{
bind
:
function
(
el
,
adapter
,
contexts
)
{
var
nodes
,
_i
,
_ref
,
_results
;
if
(
contexts
==
null
)
{
contexts
=
{};
}
};
attributeBinding
=
function
(
attr
)
{
return
function
(
el
,
value
)
{
return
setAttribute
(
el
,
attr
,
value
);
};
};
bidirectionalBindings
=
[
'value'
,
'checked'
,
'unchecked'
,
'selected'
,
'unselected'
];
return
{
bind
:
function
(
el
,
adapter
,
contexts
)
{
var
nodes
,
_i
,
_ref
,
_results
;
if
(
contexts
==
null
)
{
contexts
=
{};
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
.
attributes
.
length
-
1
;
0
<=
_ref
?
_i
<=
_ref
:
_i
>=
_ref
;
0
<=
_ref
?
_i
++
:
_i
--
){
_results
.
push
(
_i
);
}
return
_results
;
}).
apply
(
this
).
forEach
(
function
(
n
)
{
var
attribute
,
context
,
keypath
,
path
,
type
;
attribute
=
node
.
attributes
[
n
];
if
(
/^data-/
.
test
(
attribute
.
name
))
{
type
=
attribute
.
name
.
replace
(
'data-'
,
''
);
path
=
attribute
.
value
.
split
(
'.'
);
context
=
path
.
shift
();
keypath
=
path
.
join
(
'.'
);
return
registerBinding
(
node
,
adapter
,
type
,
contexts
[
context
],
keypath
);
}
});
}
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
.
attributes
.
length
-
1
;
0
<=
_ref
?
_i
<=
_ref
:
_i
>=
_ref
;
0
<=
_ref
?
_i
++
:
_i
--
){
_results
.
push
(
_i
);
}
return
_results
;
}).
apply
(
this
).
forEach
(
function
(
n
)
{
var
attribute
,
context
,
keypath
,
path
,
type
;
attribute
=
node
.
attributes
[
n
];
if
(
/^data-/
.
test
(
attribute
.
name
))
{
type
=
attribute
.
name
.
replace
(
'data-'
,
''
);
path
=
attribute
.
value
.
split
(
'.'
);
context
=
path
.
shift
();
keypath
=
path
.
join
(
'.'
);
return
registerBinding
(
node
,
adapter
,
type
,
contexts
[
context
],
keypath
);
}
});
}
});
}
};
})();
});
}
};
if
(
typeof
module
!==
"undefined"
&&
module
!==
null
)
{
module
.
exports
=
rivets
;
}
else
{
this
.
rivets
=
rivets
;
}
}).
call
(
this
);
...
...
src/rivets.coffee
View file @
d9e1bb5
...
...
@@ -3,63 +3,63 @@
# author : Michael Richards
# license : MIT
window
.
rivets
=
do
->
registerBinding
=
(
el
,
adapter
,
type
,
context
,
keypath
)
->
bind
=
bindings
[
type
]
||
attributeBinding
type
bind
el
,
adapter
.
read
context
,
keypath
registerBinding
=
(
el
,
adapter
,
type
,
context
,
keypath
)
->
bind
=
bindings
[
type
]
||
attributeBinding
type
bind
el
,
adapter
.
read
context
,
keypath
adapter
.
subscribe
context
,
keypath
,
(
value
)
->
bind
el
,
value
adapter
.
subscribe
context
,
keypath
,
(
value
)
->
bind
el
,
value
if
type
in
bidirectionalBindings
el
.
addEventListener
'change'
,
->
adapter
.
publish
context
,
keypath
,
getInputValue
this
if
type
in
bidirectionalBindings
el
.
addEventListener
'change'
,
->
adapter
.
publish
context
,
keypath
,
getInputValue
this
setAttribute
=
(
el
,
attr
,
value
,
mirrored
=
false
)
->
if
value
el
.
setAttribute
attr
,
if
mirrored
then
attr
else
value
else
el
.
removeAttribute
attr
setAttribute
=
(
el
,
attr
,
value
,
mirrored
=
false
)
->
if
value
el
.
setAttribute
attr
,
if
mirrored
then
attr
else
value
else
el
.
removeAttribute
attr
getInputValue
=
(
el
)
->
switch
el
.
type
when
'text'
,
'textarea'
,
'password'
,
'select-one'
then
el
.
value
when
'checkbox'
then
el
.
checked
getInputValue
=
(
el
)
->
switch
el
.
type
when
'text'
,
'textarea'
,
'password'
,
'select-one'
then
el
.
value
when
'checkbox'
then
el
.
checked
bindings
=
show
:
(
el
,
value
)
->
el
.
style
.
display
=
if
value
then
''
else
'none'
hide
:
(
el
,
value
)
->
el
.
style
.
display
=
if
value
then
'none'
else
''
enabled
:
(
el
,
value
)
->
setAttribute
el
,
'disabled'
,
!
value
,
true
disabled
:
(
el
,
value
)
->
setAttribute
el
,
'disabled'
,
value
,
true
checked
:
(
el
,
value
)
->
setAttribute
el
,
'checked'
,
value
,
true
unchecked
:
(
el
,
value
)
->
setAttribute
el
,
'checked'
,
!
value
,
true
selected
:
(
el
,
value
)
->
setAttribute
el
,
'selected'
,
value
,
true
unselected
:
(
el
,
value
)
->
setAttribute
el
,
'checked'
,
!
value
,
true
text
:
(
el
,
value
)
->
el
.
innerHTML
=
value
or
''
value
:
(
el
,
value
)
->
el
.
value
=
value
bindings
=
show
:
(
el
,
value
)
->
el
.
style
.
display
=
if
value
then
''
else
'none'
hide
:
(
el
,
value
)
->
el
.
style
.
display
=
if
value
then
'none'
else
''
enabled
:
(
el
,
value
)
->
setAttribute
el
,
'disabled'
,
!
value
,
true
disabled
:
(
el
,
value
)
->
setAttribute
el
,
'disabled'
,
value
,
true
checked
:
(
el
,
value
)
->
setAttribute
el
,
'checked'
,
value
,
true
unchecked
:
(
el
,
value
)
->
setAttribute
el
,
'checked'
,
!
value
,
true
selected
:
(
el
,
value
)
->
setAttribute
el
,
'selected'
,
value
,
true
unselected
:
(
el
,
value
)
->
setAttribute
el
,
'checked'
,
!
value
,
true
text
:
(
el
,
value
)
->
el
.
innerHTML
=
value
or
''
value
:
(
el
,
value
)
->
el
.
value
=
value
attributeBinding
=
(
attr
)
->
(
el
,
value
)
->
setAttribute
el
,
attr
,
value
attributeBinding
=
(
attr
)
->
(
el
,
value
)
->
setAttribute
el
,
attr
,
value
bidirectionalBindings
=
[
'value'
,
'checked'
,
'unchecked'
,
'selected'
,
'unselected'
]
bidirectionalBindings
=
[
'value'
,
'checked'
,
'unchecked'
,
'selected'
,
'unselected'
]
rivets
=
bind
:
(
el
,
adapter
,
contexts
=
{})
->
nodes
=
el
.
getElementsByTagName
'*'
[
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
]
...
...
@@ -70,3 +70,8 @@ window.rivets = do ->
context
=
path
.
shift
()
keypath
=
path
.
join
'.'
registerBinding
node
,
adapter
,
type
,
contexts
[
context
],
keypath
if
module
?
module
.
exports
=
rivets
else
@
rivets
=
rivets
\ No newline at end of file
...
...
Please
register
or
sign in
to post a comment