Skip to content

Commit 7dad3f1

Browse files
first commit
1 parent 6940e2c commit 7dad3f1

File tree

5 files changed

+189
-0
lines changed

5 files changed

+189
-0
lines changed

README

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
A library for scope declaration like activerecord 3.
2+
3+
It can be used for declare scopes and behaviors that can be attached to dom object.
4+
5+
Some examples:
6+
7+
append style css to dom objects
8+
9+
$.fn.declare.add({
10+
'set.color': function(color) {
11+
return this.css({
12+
'color': color
13+
});
14+
},
15+
'set.bgcolor': function(color) {
16+
return this.css({
17+
'background-color': color
18+
});
19+
}
20+
});
21+
22+
$("div.title").declare('set.color', 'yellow').declare('set.bgcolor', 'red');
23+
24+
25+
append events e style to table
26+
27+
$.fn.declare.add({
28+
'row.alternate': function(opt) {
29+
this.find("tr.odd")
30+
.css("background-color",opt.odd_color)
31+
.end()
32+
.find("tr.even")
33+
.css("background-color",opt.even_color);
34+
}
35+
});
36+
37+
$.fn.declare.add({
38+
'row.hover':function(color_hover) {
39+
this.hover(
40+
function() {
41+
var that=$(this);
42+
that.data("old-bg",that.css("background-color"));
43+
that.css("background-color",color_hover);
44+
},
45+
function() {
46+
var that=$(this);
47+
that.css("background-color", that.data("old-bg"));
48+
}
49+
)
50+
}
51+
});
52+
53+
$("table")
54+
.declare('row.alternate',{
55+
odd_color:'#ff0000',
56+
even_color:'#00ff00'
57+
})
58+
.find("tr")
59+
.declare("row.hover","#0000ff");
60+

examples/alternate.html

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<html>
2+
<head>
3+
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
4+
<script src="../jquery.declare.js"></script>
5+
</head>
6+
<body>
7+
8+
9+
<table>
10+
<tr class="odd"><td>row 1</td></tr>
11+
<tr class="even"><td>row 2</td></tr>
12+
<tr class="odd"><td>row 3</td></tr>
13+
</table>
14+
15+
<script>
16+
$(function() {
17+
18+
$.fn.declare.add({
19+
'row.alternate': function(opt) {
20+
this.find("tr.odd")
21+
.css("background-color",opt.odd_color)
22+
.end()
23+
.find("tr.even")
24+
.css("background-color",opt.even_color);
25+
}
26+
});
27+
28+
$.fn.declare.add({
29+
'row.hover':function(color_hover) {
30+
this.hover(
31+
function() {
32+
var that=$(this);
33+
that.data("old-bg",that.css("background-color"));
34+
that.css("background-color",color_hover);
35+
},
36+
function() {
37+
var that=$(this);
38+
that.css("background-color", that.data("old-bg"));
39+
}
40+
)
41+
}
42+
});
43+
44+
$("table")
45+
.declare('row.alternate',{
46+
odd_color:'#ff0000',
47+
even_color:'#00ff00'
48+
})
49+
.find("tr")
50+
.declare("row.hover","#0000ff");
51+
52+
});
53+
</script>
54+
55+
</body>
56+
</html>

examples/index.html

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<html>
2+
<head>
3+
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
4+
<script src="../jquery.declare.js"></script>
5+
</head>
6+
<body>
7+
<div class="title">page title</div>
8+
9+
10+
<script>
11+
$(function() {
12+
13+
$.fn.declare.add({
14+
'set.color': function(color) {
15+
return this.css({
16+
'color': color
17+
});
18+
},
19+
'set.bgcolor': function(color) {
20+
return this.css({
21+
'background-color': color
22+
});
23+
}
24+
});
25+
26+
$("div.title").declare('set.color', 'yellow').declare('set.bgcolor', 'red');
27+
28+
});
29+
</script>
30+
31+
</body>
32+
</html>

jquery.declare.coffee

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#
2+
# an example of named scope like in activerecord
3+
# implemented for jquery
4+
#
5+
#
6+
7+
do ->
8+
scopes_cache={}
9+
fn= (scope,args...) ->
10+
scopes_cache[scope].apply(this,args)
11+
return @
12+
fn.add= (scopes) -> scopes_cache[scope]=fn for scope,fn of scopes
13+
fn.remove= (scope) -> delete scopes_cache[scope]
14+
15+
jQuery.fn.declare=fn

jquery.declare.js

+26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)