From 8f1c1f419b67cfc657fa32b5d2781b8185893bfa Mon Sep 17 00:00:00 2001 From: David Poetzsch-Heffter Date: Fri, 25 Nov 2016 02:42:53 +0100 Subject: [PATCH] fixing equals on array columns in live query (#3089) --- spec/QueryTools.spec.js | 17 +++++++++++++++++ src/LiveQuery/QueryTools.js | 37 ++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/spec/QueryTools.spec.js b/spec/QueryTools.spec.js index 50433f58..16b9e784 100644 --- a/spec/QueryTools.spec.js +++ b/spec/QueryTools.spec.js @@ -226,6 +226,23 @@ describe('matchesQuery', function() { img.owner.objectId = 'U3'; expect(matchesQuery(img, q)).toBe(false); + + // pointers in arrays + q = new Parse.Query('Image'); + q.equalTo('owners', u); + + img = { + className: 'Image', + objectId: 'I1', + owners: [{ + className: '_User', + objectId: 'U2' + }] + }; + expect(matchesQuery(img, q)).toBe(true); + + img.owners[0].objectId = 'U3'; + expect(matchesQuery(img, q)).toBe(false); }); it('matches on inequalities', function() { diff --git a/src/LiveQuery/QueryTools.js b/src/LiveQuery/QueryTools.js index 4b9e7a07..b62e11bc 100644 --- a/src/LiveQuery/QueryTools.js +++ b/src/LiveQuery/QueryTools.js @@ -112,6 +112,19 @@ function matchesQuery(object: any, query: any): boolean { return true; } +function equalObjectsGeneric(obj, compareTo, eqlFn) { + if (Array.isArray(obj)) { + for (var i = 0; i < obj.length; i++) { + if (eqlFn(obj[i], compareTo)) { + return true; + } + } + return false; + } + + return eqlFn(obj, compareTo); +} + /** * Determines whether an object matches a single key's constraints @@ -143,22 +156,16 @@ function matchesKeyConstraints(object, key, constraints) { var compareTo; if (constraints.__type) { if (constraints.__type === 'Pointer') { - return ( - typeof object[key] !== 'undefined' && - constraints.className === object[key].className && - constraints.objectId === object[key].objectId - ); + return equalObjectsGeneric(object[key], constraints, function(obj, ptr) { + return ( + typeof obj !== 'undefined' && + ptr.className === obj.className && + ptr.objectId === obj.objectId + ); + }); } - compareTo = Parse._decode(key, constraints); - if (Array.isArray(object[key])) { - for (i = 0; i < object[key].length; i++) { - if (equalObjects(object[key][i], compareTo)) { - return true; - } - } - return false; - } - return equalObjects(object[key], compareTo); + + return equalObjectsGeneric(object[key], Parse._decode(key, constraints), equalObjects); } // More complex cases for (var condition in constraints) {