Skip to content

Commit 24e187d

Browse files
committed
add ability to pass options to populate and getById methods
1 parent a828f85 commit 24e187d

File tree

6 files changed

+24
-49
lines changed

6 files changed

+24
-49
lines changed

build/app.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ function ActiveRecord(model, name) {
201201
}
202202
}, {
203203
key: 'populate',
204-
value: function populate(field, query) {
204+
value: function populate(field, query, opts) {
205205
// istanbul ignore next
206206

207207
var _this2 = this;
@@ -237,7 +237,7 @@ function ActiveRecord(model, name) {
237237
if (!dao) {
238238
deferred.reject('Cannot Populate: unknown DAO');
239239
} else {
240-
return dao.get(dao.query(query).select(grouped.string)).then(function (d) {
240+
return dao.get(dao.query(query).select(grouped.string), opts).then(function (d) {
241241
/** To preserve order, we map the existing field, replacing only the populated values */
242242
self[field] = self[field].map(function (f) {
243243
if (typeof f === 'string') {
@@ -264,7 +264,7 @@ function ActiveRecord(model, name) {
264264
if (!dao) {
265265
deferred.reject('Cannot Populate: unknown DAO');
266266
} else {
267-
return dao.getById(this[field]).then(function (sub) {
267+
return dao.getById(this[field], query, opts).then(function (sub) {
268268
self[field] = sub;
269269
return self;
270270
});
@@ -808,19 +808,17 @@ function GenericDao(model, qb, discriminators) {
808808

809809
var _this2 = this;
810810

811-
var qb = arguments.length <= 1 || arguments[1] === undefined ? this.query : arguments[1];
811+
var qb = arguments.length <= 1 || arguments[1] === undefined ? this.query() : arguments[1];
812+
var opts = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
812813

813-
return this.$http.get(this.url + '/' + value, { params: qb.opts }).then(function (data) {
814+
return this.$http.get(this.url + '/' + value, _.merge(opts, { params: qb.opts })).then(function (data) {
814815
return new model(_this2.$injector, _this2.url, data.data);
815816
});
816817
};
817818
} else {
818819
myClass.prototype['selectBy' + _.capitalize(key)] = function (toSelect) {
819-
// istanbul ignore next
820-
821-
var _this3 = this;
822-
823820
var qb = arguments.length <= 1 || arguments[1] === undefined ? this.query() : arguments[1];
821+
var opts = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
824822

825823
if (toSelect && toSelect.length) {
826824
if (value.ref) {
@@ -836,14 +834,7 @@ function GenericDao(model, qb, discriminators) {
836834
}
837835
qb.setQuery(obj);
838836
}
839-
return this.$http.get(this.url, { params: qb.opts }).then(function (data) {
840-
if (!data.data) {
841-
data.data = [];
842-
}
843-
return {
844-
data: data.data.map(_this3.build, _this3), meta: { total: data.headers('X-Total-Count') }
845-
};
846-
});
837+
return this.get(qb, opts);
847838
};
848839
}
849840
//myClass.prototype

dest/temp/ActiveRecord.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ function ActiveRecord(model, name) {
200200
}
201201
}, {
202202
key: 'populate',
203-
value: function populate(field, query) {
203+
value: function populate(field, query, opts) {
204204
// istanbul ignore next
205205

206206
var _this2 = this;
@@ -236,7 +236,7 @@ function ActiveRecord(model, name) {
236236
if (!dao) {
237237
deferred.reject('Cannot Populate: unknown DAO');
238238
} else {
239-
return dao.get(dao.query(query).select(grouped.string)).then(function (d) {
239+
return dao.get(dao.query(query).select(grouped.string), opts).then(function (d) {
240240
/** To preserve order, we map the existing field, replacing only the populated values */
241241
self[field] = self[field].map(function (f) {
242242
if (typeof f === 'string') {
@@ -263,7 +263,7 @@ function ActiveRecord(model, name) {
263263
if (!dao) {
264264
deferred.reject('Cannot Populate: unknown DAO');
265265
} else {
266-
return dao.getById(this[field]).then(function (sub) {
266+
return dao.getById(this[field], query, opts).then(function (sub) {
267267
self[field] = sub;
268268
return self;
269269
});

dest/temp/GenericDao.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,17 @@ function GenericDao(model, qb, discriminators) {
164164

165165
var _this2 = this;
166166

167-
var qb = arguments.length <= 1 || arguments[1] === undefined ? this.query : arguments[1];
167+
var qb = arguments.length <= 1 || arguments[1] === undefined ? this.query() : arguments[1];
168+
var opts = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
168169

169-
return this.$http.get(this.url + '/' + value, { params: qb.opts }).then(function (data) {
170+
return this.$http.get(this.url + '/' + value, _.merge(opts, { params: qb.opts })).then(function (data) {
170171
return new model(_this2.$injector, _this2.url, data.data);
171172
});
172173
};
173174
} else {
174175
myClass.prototype['selectBy' + _.capitalize(key)] = function (toSelect) {
175-
// istanbul ignore next
176-
177-
var _this3 = this;
178-
179176
var qb = arguments.length <= 1 || arguments[1] === undefined ? this.query() : arguments[1];
177+
var opts = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
180178

181179
if (toSelect && toSelect.length) {
182180
if (value.ref) {
@@ -192,14 +190,7 @@ function GenericDao(model, qb, discriminators) {
192190
}
193191
qb.setQuery(obj);
194192
}
195-
return this.$http.get(this.url, { params: qb.opts }).then(function (data) {
196-
if (!data.data) {
197-
data.data = [];
198-
}
199-
return {
200-
data: data.data.map(_this3.build, _this3), meta: { total: data.headers('X-Total-Count') }
201-
};
202-
});
193+
return this.get(qb, opts);
203194
};
204195
}
205196
//myClass.prototype

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"author": "flogou <florent.gouget@gmail.com>",
33
"name": "angular-orm",
4-
"version": "2.0.1",
4+
"version": "2.0.2",
55
"description": "",
66
"homepage": "",
77
"dependencies": {

src/ActiveRecord.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export default function ActiveRecord (model, name, SManager = SessionManager(mod
171171
return this.$http.delete(this.rootUrl + '/' + this._id);
172172
}
173173

174-
populate (field, query) {
174+
populate (field, query, opts) {
175175
var $q = this.$injector.get('$q');
176176

177177
if (Array.isArray(field)) {
@@ -199,7 +199,7 @@ export default function ActiveRecord (model, name, SManager = SessionManager(mod
199199
if (!dao) {
200200
deferred.reject('Cannot Populate: unknown DAO');
201201
} else {
202-
return dao.get(dao.query(query).select(grouped.string)).then((d)=> {
202+
return dao.get(dao.query(query).select(grouped.string), opts).then((d)=> {
203203
/** To preserve order, we map the existing field, replacing only the populated values */
204204
self[ field ] = self[ field ].map((f) => {
205205
if (typeof f === 'string') {
@@ -226,7 +226,7 @@ export default function ActiveRecord (model, name, SManager = SessionManager(mod
226226
if (!dao) {
227227
deferred.reject('Cannot Populate: unknown DAO');
228228
} else {
229-
return dao.getById(this[ field ]).then(function (sub) {
229+
return dao.getById(this[ field ], query, opts).then(function (sub) {
230230
self[ field ] = sub;
231231
return self;
232232
});

src/GenericDao.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ export default function GenericDao(model, qb, discriminators){
111111
_.forIn(model.getModel(), function(value, key){
112112
var v = Array.isArray(value) ? value[0] : value;
113113
if (key === '_id'){
114-
myClass.prototype['findById'] = myClass.prototype['getById'] = function(value, qb = this.query){
115-
return this.$http.get(this.url + '/' + value, {params: qb.opts}).then((data)=>{
114+
myClass.prototype['findById'] = myClass.prototype['getById'] = function(value, qb = this.query(), opts = {}){
115+
return this.$http.get(this.url + '/' + value, _.merge(opts, {params: qb.opts})).then((data)=>{
116116
return new model(this.$injector, this.url, data.data);
117117
})
118118
}
119119
} else{
120-
myClass.prototype['selectBy' + _.capitalize(key)] = function(toSelect, qb = this.query()){
120+
myClass.prototype['selectBy' + _.capitalize(key)] = function(toSelect, qb = this.query(), opts={}){
121121
if (toSelect && toSelect.length){
122122
if (value.ref){
123123
toSelect = extractId(toSelect);
@@ -132,14 +132,7 @@ export default function GenericDao(model, qb, discriminators){
132132
}
133133
qb.setQuery(obj);
134134
}
135-
return this.$http.get(this.url, {params: qb.opts}).then((data)=>{
136-
if (!data.data) {
137-
data.data = []
138-
}
139-
return {
140-
data: data.data.map(this.build, this), meta: {total: data.headers('X-Total-Count')}
141-
};
142-
})
135+
return this.get(qb, opts)
143136
}
144137

145138
}

0 commit comments

Comments
 (0)