-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
75 lines (62 loc) · 1.3 KB
/
test.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
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
function Thing(name) {
this.name = name;
}
Thing.prototype.doSomething = function(callback, salutation) {
// Call our callback, but using our own instance as the context
callback.call(this, salutation);
}
function foo(salutation) {
alert(salutation + " " + this.name);
}
function Document(){}
Document.prototype = {
_id: 0,
_name: '',
name: function() {
return this._name;
},
id: function() {
return this._id;
},
save: function() {
return true;
},
open: function(id) {
this._id = id;
this._name = 'Ajax on AOP steroids'
alert(this._name);
return true;
}
}
function Lockable(){}
Lockable.prototype = {
_locked: false,
locked: function() {
return this._locked;
},
lock: function() {
this._locked = true;
}
}
var lockable = new Lockable();
jQuery.aop.around( {target: Document, method: 'open'},
function(invocation) {
alert('open: ' + invocation.arguments[0]);
var result = "";
if(lockable.locked() === false) {
result= invocation.proceed();
lockable.lock();
}
else result = false;
return result;
}
);
var t = new Thing('Joe');
t.doSomething(foo, 'Hi;'); // Alerts "Hi Joe" via `foo`
doc = new Document();
var test = doc.open("i1");
if(test) alert("opened");
else alert("locked");
var test = doc.open("i2");
if(test) alert("opened");
else alert("locked");