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
b1b0aa9a
authored
2012-05-05 16:38:31 -0700
by
Michael Richards
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Implement full node/attribute parsing.
1 parent
a8c387d3
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
31 deletions
lib/rivets.js
src/rivets.coffee
lib/rivets.js
View file @
b1b0aa9
window
.
rivets
=
(
function
()
{
var
attr
,
bindableAttributes
,
bindings
,
getInputValue
,
registerBinding
,
setAttribute
,
_fn
,
_i
,
_len
;
registerBinding
=
function
(
el
,
interface
,
contexts
,
type
,
callback
)
{
return
$
(
"*[data-"
+
type
+
"]"
,
el
).
each
(
function
()
{
var
context
,
inputValue
,
keypath
,
path
;
var
_this
=
this
;
path
=
$
(
this
).
attr
(
"data-"
+
type
).
split
(
'.'
);
context
=
path
.
shift
();
keypath
=
path
.
join
(
'.'
);
callback
(
this
,
interface
.
read
(
contexts
[
context
],
keypath
));
interface
.
subscribe
(
contexts
[
context
],
keypath
,
function
(
value
)
{
return
callback
(
_this
,
value
);
registerBinding
=
function
(
el
,
interface
,
type
,
context
,
keypath
)
{
var
inputValue
;
bindings
[
type
](
el
,
interface
.
read
(
context
,
keypath
));
interface
.
subscribe
(
context
,
keypath
,
function
(
value
)
{
return
bindings
[
type
](
el
,
value
);
});
if
(
inputValue
=
getInputValue
(
this
))
{
return
interface
.
publish
(
contexts
[
context
]
,
keypath
,
inputValue
);
if
(
inputValue
=
getInputValue
(
el
))
{
return
interface
.
publish
(
context
,
keypath
,
inputValue
);
}
});
};
setAttribute
=
function
(
el
,
attr
,
value
,
mirrored
)
{
if
(
mirrored
==
null
)
mirrored
=
false
;
...
...
@@ -88,14 +82,31 @@
}
return
{
bind
:
function
(
el
,
interface
,
contexts
)
{
var
callback
,
type
,
_results
;
if
(
contexts
==
null
)
contexts
=
{};
return
$
(
el
).
add
(
$
(
'*'
,
el
)).
each
(
function
()
{
var
nodeMap
,
target
,
_j
,
_ref
,
_results
;
target
=
this
;
nodeMap
=
target
.
attributes
;
if
(
nodeMap
.
length
>
0
)
{
return
(
function
()
{
_results
=
[];
for
(
type
in
bindings
)
{
callback
=
bindings
[
type
];
_results
.
push
(
registerBinding
(
el
,
interface
,
contexts
,
type
,
callback
));
}
for
(
var
_j
=
0
,
_ref
=
nodeMap
.
length
-
1
;
0
<=
_ref
?
_j
<=
_ref
:
_j
>=
_ref
;
0
<=
_ref
?
_j
++
:
_j
--
){
_results
.
push
(
_j
);
}
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
(
_
.
include
(
_
.
keys
(
bindings
),
type
))
{
path
=
node
.
value
.
split
(
'.'
);
context
=
path
.
shift
();
keypath
=
path
.
join
(
'.'
);
return
registerBinding
(
$
(
target
),
interface
,
type
,
contexts
[
context
],
keypath
);
}
}
});
}
});
}
};
})();
...
...
src/rivets.coffee
View file @
b1b0aa9
...
...
@@ -4,19 +4,14 @@
# license : MIT
window
.
rivets
=
do
->
registerBinding
=
(
el
,
interface
,
contexts
,
type
,
callback
)
->
$
(
"*[data-
#{
type
}
]"
,
el
).
each
->
path
=
$
(
this
).
attr
(
"data-
#{
type
}
"
).
split
'.'
context
=
path
.
shift
()
keypath
=
path
.
join
'.'
registerBinding
=
(
el
,
interface
,
type
,
context
,
keypath
)
->
bindings
[
type
]
el
,
interface
.
read
(
context
,
keypath
)
callback
this
,
interface
.
read
contexts
[
context
],
keypath
interface
.
subscribe
context
,
keypath
,
(
value
)
->
bindings
[
type
]
el
,
value
interface
.
subscribe
contexts
[
context
],
keypath
,
(
value
)
=>
callback
this
,
value
if
inputValue
=
getInputValue
this
interface
.
publish
contexts
[
context
],
keypath
,
inputValue
if
inputValue
=
getInputValue
el
interface
.
publish
context
,
keypath
,
inputValue
setAttribute
=
(
el
,
attr
,
value
,
mirrored
=
false
)
->
if
value
...
...
@@ -59,5 +54,19 @@ window.rivets = do ->
setAttribute
el
,
attr
,
value
bind
:
(
el
,
interface
,
contexts
=
{})
->
for
type
,
callback
of
bindings
registerBinding
el
,
interface
,
contexts
,
type
,
callback
\ No newline at end of file
$
(
el
).
add
(
$
(
'*'
,
el
)).
each
->
target
=
this
nodeMap
=
target
.
attributes
if
nodeMap
.
length
>
0
[
0
..(
nodeMap
.
length
-
1
)].
forEach
(
n
)
->
node
=
nodeMap
[
n
]
if
/^data-/
.
test
node
.
name
type
=
node
.
name
.
replace
'data-'
,
''
if
_
.
include
_
.
keys
(
bindings
),
type
path
=
node
.
value
.
split
'.'
context
=
path
.
shift
()
keypath
=
path
.
join
'.'
registerBinding
$
(
target
),
interface
,
type
,
contexts
[
context
],
keypath
...
...
Please
register
or
sign in
to post a comment