From 45b662a1ddb46a0f17fab7b2383c82aa1e1620ef Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Tue, 8 Sep 2015 20:19:42 +0200 Subject: Centralize all mutations at the model level This commit uses a new package that I need to document. It tries to solve the long-standing debate in the Meteor community about allow/deny rules versus methods (RPC). This approach gives us both the centralized security rules of allow/deny and the white-list of allowed mutations similarly to Meteor methods. The idea to have static mutation descriptions is also inspired by Facebook's Relay/GraphQL. This will allow the development of a REST API using the high-level methods instead of the MongoDB queries to do the mapping between the HTTP requests and our collections. --- client/components/sidebar/sidebarFilters.js | 49 +++++++++++------------------ 1 file changed, 18 insertions(+), 31 deletions(-) (limited to 'client/components/sidebar/sidebarFilters.js') diff --git a/client/components/sidebar/sidebarFilters.js b/client/components/sidebar/sidebarFilters.js index 335cc7d6..babd2f1e 100644 --- a/client/components/sidebar/sidebarFilters.js +++ b/client/components/sidebar/sidebarFilters.js @@ -30,9 +30,9 @@ BlazeComponent.extendComponent({ }, }).register('filterSidebar'); -function updateSelectedCards(query) { +function mutateSelectedCards(mutationName, ...args) { Cards.find(MultiSelection.getMongoSelector()).forEach((card) => { - Cards.update(card._id, query); + card[mutationName](...args); }); } @@ -67,47 +67,34 @@ BlazeComponent.extendComponent({ 'click .js-toggle-label-multiselection'(evt) { const labelId = this.currentData()._id; const mappedSelection = this.mapSelection('label', labelId); - let operation; - if (_.every(mappedSelection)) - operation = '$pull'; - else if (_.every(mappedSelection, (bool) => !bool)) - operation = '$addToSet'; - else { + + if (_.every(mappedSelection)) { + mutateSelectedCards('addLabel', labelId); + } else if (_.every(mappedSelection, (bool) => !bool)) { + mutateSelectedCards('removeLabel', labelId); + } else { const popup = Popup.open('disambiguateMultiLabel'); // XXX We need to have a better integration between the popup and the // UI components systems. return popup.call(this.currentData(), evt); } - - updateSelectedCards({ - [operation]: { - labelIds: labelId, - }, - }); }, 'click .js-toggle-member-multiselection'(evt) { const memberId = this.currentData()._id; const mappedSelection = this.mapSelection('member', memberId); - let operation; - if (_.every(mappedSelection)) - operation = '$pull'; - else if (_.every(mappedSelection, (bool) => !bool)) - operation = '$addToSet'; - else { + if (_.every(mappedSelection)) { + mutateSelectedCards('assignMember', memberId); + } else if (_.every(mappedSelection, (bool) => !bool)) { + mutateSelectedCards('unassignMember', memberId); + } else { const popup = Popup.open('disambiguateMultiMember'); // XXX We need to have a better integration between the popup and the // UI components systems. return popup.call(this.currentData(), evt); } - - updateSelectedCards({ - [operation]: { - members: memberId, - }, - }); }, 'click .js-archive-selection'() { - updateSelectedCards({$set: {archived: true}}); + mutateSelectedCards('archive'); }, }]; }, @@ -115,22 +102,22 @@ BlazeComponent.extendComponent({ Template.disambiguateMultiLabelPopup.events({ 'click .js-remove-label'() { - updateSelectedCards({$pull: {labelIds: this._id}}); + mutateSelectedCards('removeLabel', this._id); Popup.close(); }, 'click .js-add-label'() { - updateSelectedCards({$addToSet: {labelIds: this._id}}); + mutateSelectedCards('addLabel', this._id); Popup.close(); }, }); Template.disambiguateMultiMemberPopup.events({ 'click .js-unassign-member'() { - updateSelectedCards({$pull: {members: this._id}}); + mutateSelectedCards('assignMember', this._id); Popup.close(); }, 'click .js-assign-member'() { - updateSelectedCards({$addToSet: {members: this._id}}); + mutateSelectedCards('unassignMember', this._id); Popup.close(); }, }); -- cgit v1.2.3-1-g7c22