-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst_bridge.js
40 lines (30 loc) · 947 Bytes
/
st_bridge.js
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
// Brdige Design Pattern.
// The Bridge pattern should “decouple an abstraction from its implementation so that the two can vary independently.” Bridges are quite beneficial in event-driven applications,
/*
http://www.joezimjs.com/javascript/javascript-design-patterns-bridge/
*/
// BAD CODE, as this code can only be used only at a single place.
getXById = function() {
var id = this.id;
$.ajax({
url:'/getx?id=' + id,
success: function(response) {
console.log(response);
}
});
}
$('someElement').bind('click', getXById);
// Correct Code , now getXById can also be used somewhere else.
getXById = function(id, callback) {
$.ajax({
url:'/getx?id=' + id,
success: callback
}
}
getXBridge = function() {
var id = this.id;
getXById(this.id, function() {
console.log(response);
});
}
$('someElement').bind('click', getXBridge);