From 2e9e2dd192aba7c9f7bf6cebb80b16a2411e8a71 Mon Sep 17 00:00:00 2001 From: Clark Wang Date: Thu, 22 Jan 2015 10:39:47 +0800 Subject: [PATCH] fix id property for composite ids Signed-off-by: Clark Wang --- lib/model-builder.js | 3 ++- test/loopback-dl.test.js | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/model-builder.js b/lib/model-builder.js index 8af0a02d..eec0034a 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -266,7 +266,8 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett get: function () { var compositeId = {}; var idNames = ModelClass.definition.idNames(); - for (var p in idNames) { + for (var i = 0, p; i < idNames.length; i++) { + p = idNames[i]; compositeId[p] = this.__data[p]; } return compositeId; diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index 41d600f6..1d570d13 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -243,6 +243,19 @@ describe('ModelBuilder define model', function () { done(null, User); }); + it('should define an id property for composite ids', function () { + var modelBuilder = new ModelBuilder(); + var Follow = modelBuilder.define('Follow', { + followerId: { type: String, id: 1 }, + followeeId: { type: String, id: 2 }, + followAt: Date + }); + var follow = new Follow({ followerId: 1, followeeId: 2 }); + + follow.should.have.property('id'); + assert.deepEqual(follow.id, { followerId: 1, followeeId: 2 }); + }); + }); describe('DataSource ping', function() {