From 79d76ea8b3ee9638b846415fbe670fa07f76922a Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Mon, 19 Dec 2022 14:31:32 -0500 Subject: [PATCH 1/2] added tutorial to getters-setters --- docs/tutorials/getters-setters.md | 8 +++++ test/docs/getters-setters.test.js | 49 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/docs/tutorials/getters-setters.md b/docs/tutorials/getters-setters.md index dc94fb6a41b..31d2b3ce247 100644 --- a/docs/tutorials/getters-setters.md +++ b/docs/tutorials/getters-setters.md @@ -81,3 +81,11 @@ corresponding getter for `email`. ```javascript [require:getters/setters.*setters.*vs ES6] ``` + +## $locals + +You can use `$locals` to support internationalization for your application. + +```javascript +[require:getters/setters.*localization.*locale] +``` diff --git a/test/docs/getters-setters.test.js b/test/docs/getters-setters.test.js index 80c7d1799a9..1c5c89bb610 100644 --- a/test/docs/getters-setters.test.js +++ b/test/docs/getters-setters.test.js @@ -174,4 +174,53 @@ describe('getters/setters', function() { // acquit:ignore:end }); }); + describe('localization', function() { + it('locale', async function() { + const internationalizedStringSchema = new Schema({ + en: String, + es: String + }); + + const ingredientSchema = new Schema({ + // Instead of setting `name` to just a string, set `name` to a map + // of language codes to strings. + name: { + type: internationalizedStringSchema, + // When you access `name`, pull the document's locale + get: function(value) { + return value[this.$locals.language || 'en']; + } + } + }); + + const recipeSchema = new Schema({ + ingredients: [{ type: mongoose.ObjectId, ref: 'Ingredient' }] + }); + + const Ingredient = mongoose.model('Ingredient', ingredientSchema); + const Recipe = mongoose.model('Recipe', recipeSchema); + + // Create some sample data + const { _id } = await Ingredient.create({ + name: { + en: 'Eggs', + es: 'Huevos' + } + }); + await Recipe.create({ ingredients: [_id] }); + + // Populate with setting `$locals.language` for internationalization + const language = 'es'; + const recipes = await Recipe.find().populate({ + path: 'ingredients', + transform: function(doc) { + doc.$locals.language = language; + return doc; + } + }); + + // Gets the ingredient's name in Spanish `name.es` + assert.equal(recipes[0].ingredients[0].name, 'Huevos'); // 'Huevos' + }); + }) }); \ No newline at end of file From b50148eec5ad6f210e567bb253457a2c58648541 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 19 Dec 2022 15:32:15 -0500 Subject: [PATCH 2/2] Update getters-setters.md --- docs/tutorials/getters-setters.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/docs/tutorials/getters-setters.md b/docs/tutorials/getters-setters.md index 31d2b3ce247..48aebce9182 100644 --- a/docs/tutorials/getters-setters.md +++ b/docs/tutorials/getters-setters.md @@ -70,6 +70,19 @@ below. [require:getters/setters.*setters.*update skip] ``` +## Passing Parameters using `$locals` + +You can't pass parameters to your getter and setter functions like you do to normal function calls. +To configure or pass additional properties to your getters and setters, you can use the document's `$locals` property. + +The `$locals` property is the preferred place to store any program-defined data on your document without conflicting with schema-defined properties. +In your getter and setter functions, `this` is the document being accessed, so you set properties on `$locals` and then access those properties in your getters examples. +For example, the following shows how you can use `$locals` to configure the language for a custom getter that returns a string in different languages. + +```javascript +[require:getters/setters.*localization.*locale] +``` + Differences vs ES6 Getters/Setters ---------------------------------- @@ -81,11 +94,3 @@ corresponding getter for `email`. ```javascript [require:getters/setters.*setters.*vs ES6] ``` - -## $locals - -You can use `$locals` to support internationalization for your application. - -```javascript -[require:getters/setters.*localization.*locale] -```