Skip to content

Commit 8a4ad5e

Browse files
authored
Merge pull request #284 from mrbatista/bug/skip-last-method
Create custom method generate endpoint without fk param
2 parents d1c1f75 + 64ec05e commit 8a4ad5e

File tree

2 files changed

+103
-6
lines changed

2 files changed

+103
-6
lines changed

lib/services.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,22 @@ function describeModels(app, options) {
157157
return;
158158
}
159159

160+
var createMethod = c.methods.filter(function(method) {
161+
return method.name === 'create';
162+
});
163+
164+
if (createMethod && createMethod.length === 1) {
165+
var createMany = Object.create(createMethod[0]);
166+
createMany.name = 'createMany';
167+
createMany.isReturningArray = function() { return true; };
168+
c.methods.push(createMany);
169+
}
170+
160171
// The URL of prototype methods include sharedCtor parameters like ":id"
161172
// Because all $resource methods are static (non-prototype) in ngResource,
162173
// the sharedCtor parameters should be added to the parameters
163174
// of prototype methods.
164175
c.methods.forEach(function fixArgsOfPrototypeMethods(method, key) {
165-
if (method.name == 'create') {
166-
var createMany = Object.create(method);
167-
createMany.name = 'createMany';
168-
createMany.isReturningArray = function() { return true; };
169-
c.methods.splice(key + 1, 0, createMany);
170-
}
171176
var ctor = method.restClass.ctor;
172177
if (!ctor || method.sharedMethod.isStatic) return;
173178
method.accepts = ctor.accepts.concat(method.accepts);

test.e2e/spec/services.spec.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,98 @@ define(['angular', 'given', 'util'], function(angular, given, util) {
13971397
expect(IgnoredModel).to.be.false;
13981398
});
13991399
});
1400+
1401+
describe('inherit params for custom method that extends relation',
1402+
function() {
1403+
var $injector, Person, testData;
1404+
before(function() {
1405+
return given.servicesForLoopBackApp(
1406+
{
1407+
models: {
1408+
Person: {
1409+
properties: { name: 'string' },
1410+
options: {
1411+
relations: {
1412+
_addresses: {
1413+
model: 'Address',
1414+
type: 'embedsMany',
1415+
property: 'addresses',
1416+
},
1417+
},
1418+
},
1419+
},
1420+
Address: {
1421+
properties: { name: 'string' },
1422+
},
1423+
},
1424+
name: 'custom_method',
1425+
setupFn: (function(app, cb) {
1426+
/*globals debug:true */
1427+
// eslint-disable-next-line camelcase
1428+
app.models.Person.prototype.__custom___address =
1429+
function(fk, cb) { return cb(null, { name: 'custom' }); };
1430+
app.models.Person.remoteMethod('prototype.__custom___address', {
1431+
accepts: [
1432+
{
1433+
arg: 'fk',
1434+
type: 'any',
1435+
required: true,
1436+
http: { source: 'path' },
1437+
},
1438+
],
1439+
returns: [
1440+
{
1441+
arg: 'data',
1442+
type: 'object',
1443+
root: true,
1444+
},
1445+
],
1446+
http: {
1447+
path: '/_address/:fk/custom',
1448+
verb: 'post',
1449+
},
1450+
});
1451+
app.models.Person.create(
1452+
{ name: 'Matteo' },
1453+
function(err, person) {
1454+
if (err) return cb(err);
1455+
debug('Created person', person);
1456+
1457+
person._addresses.create({ name: 'Turin' },
1458+
function(err, address) {
1459+
if (err) return cb(err);
1460+
debug('Created address', address);
1461+
1462+
person._addresses(true, function(err, res) {
1463+
if (err) return cb(err);
1464+
debug('Address of the person', res);
1465+
1466+
cb(null, {
1467+
person: person,
1468+
address: address,
1469+
});
1470+
});
1471+
}
1472+
);
1473+
}
1474+
);
1475+
}).toString(),
1476+
})
1477+
.then(function(createInjector) {
1478+
$injector = createInjector();
1479+
Person = $injector.get('Person');
1480+
testData = $injector.get('testData');
1481+
});
1482+
});
1483+
1484+
it('create correct endpoint for custom method', function() {
1485+
var customPerson = Person.prototype$__custom___address(
1486+
{ id: testData.person.id, fk: testData.address.id });
1487+
return customPerson.$promise.then(function() {
1488+
expect(customPerson.name).to.be.equal('custom');
1489+
});
1490+
});
1491+
});
14001492
});
14011493

14021494
function propGetter(name) {

0 commit comments

Comments
 (0)