forked from shimondoodkin/nodejs-mongodb-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicapp_subitems.js
executable file
·156 lines (140 loc) · 5.24 KB
/
basicapp_subitems.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
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
this.main=function main(app)
{
app.prepare_subitems_lists = function prepare_subitems_lists(main_model)
{
//sys.puts(sys.inspect(main_model));
var operation,operations=['edit','view'];
var operations_copy={'list':'view','add':'edit','multiupdate':'edit','advancedsearch':'edit'};
var arrselections,fieldname,havelookup,lookupinfo,field,operations_type;
main_model.prep_subitems={};
for(var i=0;i<operations.length;i++)
{
operation=operations[i];
arrselections=[];
operations_type=operation;
if(operation=='view') operations_type='view';
else if(operation=='list') operations_type='view';
else if(operation=='add') operations_type='edit';
else if(operation=='multiupdate') operations_type='edit';
else if(operation=='advancedsearch') operations_type='edit'; // add searchtags later
for(fieldname in main_model.fields)
{
if(!main_model.fields[fieldname]) console.log('field \''+main_model.general.name +','+fieldname+'\' not found')
field=main_model.fields[fieldname];
if(operations_type=='view')
{
if(typeof field.viewtag[
field[operations_type].ftype
].lookup===null)
{
havelookup=field.edittag[
field['edit'].ftype
].lookup
?true:false;
if(havelookup)
lookupinfo=field.edittag.lookup;
}
else
{
havelookup=field.edittag[
field[operations_type].ftype
].lookup
?true:false;
if(havelookup)
{
if(field.viewtag.lookup.sameasedit)
lookupinfo=field.edittag.lookup;
else
{
lookupinfo=field.viewtag.lookup;
}
}
}
}
else
{
//console.log(operations_type);
//console.log(sys.inspect(field));
havelookup=field.edittag[ field[operations_type].ftype ].lookup?true:false;
if(havelookup)
{
lookupinfo=field.edittag.lookup;
}
}
if(havelookup)
{
arrselections.push({where:(lookupinfo.where?lookupinfo.where:false), submodel:app.models[lookupinfo.tablename],'fieldname':fieldname,cursor:[],'lookupinfo':lookupinfo});
}
}
main_model.prep_subitems[operation]=arrselections;
}
var source,target;
for(target in operations_copy)
{
source=operations_copy[target];
main_model.prep_subitems[target]=main_model.prep_subitems[source];
}
//console.log(sys.inspect(main_model.prep_subitems));
}
app.load_subitems = function load_subitems(main_model,operation,callback)
{ //shoud i add static support ? app.load_subitems( page.model , 'static/cached/on change of a specific model(onchange is multi process unsupported)') ? <<should i add this later?
if(main_model.prep_subitems[operation].length==0)callback({});
//console.log(main_model.modelname+','+operation);
//console.log(main_model.modelname+"|"+operation+"| "+sys.inspect(main_model.prep_subitems[operation]));
var arrselections=app._.cloneuptolevel(main_model.prep_subitems[operation],2)
app.step(
function ()
{
//console.log('start get items for a all sub items collection');
var new_group = this.group();
//console.log(sys.inspect(arrselections,false,1));
arrselections.forEach(function (selection,i,arr)
//for(var i=0,l=arrselections.length;i<l;i++)
{
//console.log('start get items for a subitem collection '+selection.fieldname);
var ret_group=new_group(); // add + 1 count to wait for results list
//console.log('select sub items: selections.'+selection.fieldname+'.fieldname = '+selection.fieldname);
if(!selection.where)selection.where=null;
selection.submodel.select(selection.where,function (cursor)
{
try{
// console.log('ret list' + sys.inspect(cursor));
cursor.toArray(function(err, items)
{
///console.log('recevied array');
//console.log('received subitems '+selection.fieldname);
//console.log(sys.inspect(items));
selection.cursor=items;
ret_group(null,true);
});
}
catch(e)
{
//console.log('error in load_subitems');
//(new_group())(null,true);
ret_group(null,false);
}
});
});
//console.log('step 100');
//this.next();
},
function (err, contents)
{
// console.log('end');
//if(err)
// console.log('step 200 ='+err);
//else
// console.log('step 200 ='+contents);
err=null;
//console.log(sys.inspect(arrselections))
var byfield={};
for(var i=0,l=arrselections.length;i<l;i++)
byfield[arrselections[i].fieldname]=arrselections[i];
//if (err) { throw err; }
arrselections=null;
callback(byfield);
}
);
}
}