-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathadapter.js
More file actions
113 lines (93 loc) · 1.9 KB
/
adapter.js
File metadata and controls
113 lines (93 loc) · 1.9 KB
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
function Adapter(obj) {
if (!(this instanceof Adapter)) {
return new Adapter(obj);
}
var self = this;
self.obj = obj;
};
/**
* Default subscription method.
* Subscribe to changes on the model.
*
* @param {Object} obj
* @param {String} prop
* @param {Function} fn
*/
Adapter.prototype.subscribe = function(prop, fn) {
};
/**
* Default unsubscription method.
* Unsubscribe from changes on the model.
*/
Adapter.prototype.unsubscribe = function(prop, fn) {
};
/**
* Remove all subscriptions on this adapter
*/
Adapter.prototype.unsubscribeAll = function() {
};
/**
* Default setter method.
* Set a property on the model.
*
* @param {Object} obj
* @param {String} prop
* @param {Mixed} val
*/
Adapter.prototype.set = function(prop, val) {
var obj = this.obj;
var parts = prop.split('.')
var part = parts.shift()
if (!obj) return;
// split property on '.' access
// and dig into the object
while (parts.length) {
// create the nested property if it doesn't exist
if (obj[part] === undefined) {
obj[part] = {};
}
obj = obj[part];
part = prop = parts.shift();
};
if ('function' == typeof obj[prop]) {
obj[prop](val);
}
else if ('function' == typeof obj.set) {
obj.set(prop, val);
}
else {
obj[prop] = val;
}
};
/**
* Default getter method.
* Get a property from the model.
*
* @param {Object} obj
* @param {String} prop
* @return {Mixed}
*/
Adapter.prototype.get = function(prop) {
var obj = this.obj;
if (!obj) {
return undefined;
}
// split property on '.' access
// and dig into the object
var parts = prop.split('.');
var part = parts.shift();
do {
if (typeof obj[part] === 'function') {
obj = obj[part].call(obj);
}
else {
obj = obj[part];
}
if (!obj) {
return undefined;
}
part = parts.shift();
} while(part);
return obj;
};
module.exports = Adapter;