-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseEntityManager.j
183 lines (160 loc) · 4.68 KB
/
BaseEntityManager.j
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// (c) 2010-2011 by Anton Korenyushkin
@import "BaseManager.j"
@import "HTTPRequest.j"
@import "EntityDeleting.j"
@implementation BaseEntityManager : BaseManager
{
BOOL isLoading;
id revealTarget @accessors;
SEL revealAction @accessors;
}
- (void)load // public
{
isLoading = YES;
[[[HTTPRequest alloc] initWithMethod:"GET" URL:[self URL] target:self action:@selector(didReceiveRepr:)] send];
}
- (void)didReceiveRepr:(JSObject)repr // private
{
isLoading = NO;
[self processRepr:repr];
}
- (BOOL)isExpandable // public
{
return YES;
}
- (void)revealItems:(CPArray)items // protected
{
objj_msgSend(revealTarget, revealAction, items);
}
- (void)insertNewItem:(id)item // protected
{
item.isEditable = YES;
item.manager = self;
[self insertItem:item];
[self notify];
[self revealItems:[item]];
}
- (void)markRenameItem:(id)item // public
{
item.isEditable = YES;
item.didExist = YES;
item.manager = self;
}
- (void)submitItem:(id)item withName:(CPString)name // public
{
delete item.isEditable;
delete item.manager;
if (item.didExist) {
delete item.didExist;
if (name && name != item.name)
[self renameItem:item to:name];
} else {
[self createItem:item withName:name];
}
[self notify];
[self revealItems:[item]];
}
- (void)requestWithMethod:(CPString)method
URL:(CPString)url
data:(JSObject)data
selector:(SEL)selector
errorSelector:(SEL)errorSelector
args:(JSObject)args // protected
{
var request = [[HTTPRequest alloc] initWithMethod:method
URL:url
target:self
action:@selector(didReceiveResponse:withContext:)];
[request setErrorAction:@selector(didReceiveError:withContext:)];
[request setContext:{selector: selector, errorSelector: errorSelector, args: args}];
[request send:data];
}
- (void)didReceiveResponse:(JSObject)data withContext:(JSObject)context // private
{
objj_msgSend.apply(nil, [self, context.selector].concat(context.args));
}
- (void)didReceiveError:(JSObject)data withContext:(JSObject)context // private
{
objj_msgSend.apply(nil, [self, context.errorSelector].concat(context.args));
}
- (void)createItem:(id)item byRequestWithMethod:(CPString)method URL:(CPString)url data:(JSObject)data // protected
{
item.isLoading = YES;
[self requestWithMethod:method
URL:url
data:data
selector:@selector(didCreateItem:)
errorSelector:@selector(didFailToCreateItem:)
args:[item]];
}
- (void)didCreateItem:(id)item // protected
{
delete item.isLoading;
[self notify];
}
- (void)didFailToCreateItem:(id)item // private
{
[self removeItem:item];
[item noteDeleted];
[self notify];
}
- (void)changeNameOfItem:(id)item to:(CPString)name // protected
{
[item setName:name];
[self removeItem:item];
[self insertItem:item];
}
- (void)renameItem:(id)item
to:(CPString)name
byRequestWithMethod:(CPString)method
URL:(CPString)url
data:(JSObject)data // protected
{
item.isLoading = YES;
item.oldName = item.name;
[self changeNameOfItem:item to:name];
[self requestWithMethod:method
URL:url
data:data
selector:@selector(didRenameItem:)
errorSelector:@selector(didFailToRenameItem:)
args:[item]];
}
- (void)didRenameItem:(id)item // private
{
delete item.isLoading;
delete item.oldName;
[self notify];
}
- (void)didFailToRenameItem:(id)item // private
{
[self changeNameOfItem:item to:item.oldName];
delete item.isLoading;
delete item.oldName;
[self notify];
}
- (void)deleteItems:(CPArray)items byRequestWithMethod:(CPString)method URL:(CPString)url data:(JSObject)data // protected
{
items.forEach(function (item) { item.isLoading = YES; });
[self requestWithMethod:method
URL:url
data:data
selector:@selector(didDeleteItems:)
errorSelector:@selector(didFailToDeleteItems:)
args:[items]];
}
- (void)didDeleteItems:(CPArray)items // private
{
items.forEach(
function (item) {
[self removeItem:item];
[item noteDeleted];
});
[self notify];
}
- (void)didFailToDeleteItems:(CPArray)items // private
{
items.forEach(function (item) { delete item.isLoading; });
[self notify];
}
@end