-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataBinder-js.html
133 lines (95 loc) · 2.99 KB
/
dataBinder-js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Jquery Ng</title>
<!--script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.8.0/jquery.min.js"></script-->
<script type="text/javascript">
function DataBinder(object_id) {
// Create a simple PubSub object
var pubSub = {
callbacks: {},
on: function(msg, callback) {
this.callbacks[msg] = this.callbacks[msg] || [];
this.callbacks[msg].push(callback);
},
publish: function(msg) {
this.callbacks[msg] = this.callbacks[msg] || []
for (var i = len = this.callbacks[msg].length; i < len; i++) {
this.callbacks[msg][i].apply(this, arguments);
}
}
},
data_attr = "data-bind-" + object_id,
message = object_id + ":change",
changeHandler = function(evt) {
var target = evt.target || evt.srcElement, // IEcompatibility
prop_name = target.getAttribute(data_attr);
if (prop_name && prop_name !== "") {
pubSub.publish(message, prop_name, target.value);
}
};
// Listen to change events and proxy to PubSub
if (document.addEventListener) {
document.addEventListener("change", changeHandler, false);
} else {
// IEuses attachEvent instead of addEventListener
document.attachEvent("onchange", changeHandler);
}
// PubSub propagates changes to all bound elements
pubSub.on(message, function(evt, prop_name, new_val) {
var elements = document.querySelectorAll("[" + data_attr + "=" + prop_name + "]"),
tag_name;
for (var i = len = elements.length; i < len; i++) {
tag_name = elements[i].tagName.toLowerCase();
if (tag_name === "input" || tag_name === "textarea" || tag_name === "select") {
elements[i].value = new_val;
} else {
elements[i].innerHTML = new_val;
}
}
});
return pubSub;
}
function User( uid ) {
var binder = new DataBinder( uid ),
user = {
attributes: {},
// The attribute setter publish changes using the DataBinder PubSub
set: function( attr_name, val ) { // name 345
this.attributes[ attr_name ] = val;
binder.publish( uid + ":change", attr_name, val, this );
},
get: function( attr_name ) {
return this.attributes[ attr_name ];
},
_binder: binder
};
// Subscribe to the PubSub
binder.on( uid + ":change", function( evt, attr_name, new_val, initiator ) {
if ( initiator !== user ) {
user.set( attr_name, new_val );
}
});
return user;
}
</script>
</head>
<body>
<input type="number" data-bind-123="name" />
<input type="number" data-bind-123="name" />
<input type="number" data-bind-123="name" />
<input type="number" data-bind-123="name" />
<span data-bind-123="name"></span>
<script type="text/javascript">
var user = new User( 123 );
user.set( "name", "345" );
n = 0 ;
setInterval(function() {
// user.set( "name",n)
// n ++
// console.log(user.get('name'));
}, 4000)
</script>
</body>
</html>