From 9abeab0a1af46c1c73b6b0ba686089eb66fe9111 Mon Sep 17 00:00:00 2001 From: Bryce Johnson Date: Fri, 21 Apr 2017 01:30:23 +0000 Subject: Document information hiding best practices in Vue frontend guide. --- doc/development/fe_guide/vue.md | 85 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/doc/development/fe_guide/vue.md b/doc/development/fe_guide/vue.md index 45c8300d9de..ceb46428894 100644 --- a/doc/development/fe_guide/vue.md +++ b/doc/development/fe_guide/vue.md @@ -290,6 +290,91 @@ new Vue({ The [issue boards service][issue-boards-service] is a good example of this pattern. +## Information hiding + +To make components more reusable, readable, and performant, aim to only pass props that +the child component needs. + +Instead of this: + +``` +const ParentComponent = { + data: bigGiantObject, + template: ``, +} + +const ChildComponent = { + props: { + bigGiantObject: { type: Object } + }, + template: ` +
+ {{ bigGiantObject.littlePropertyOne }} + {{ bigGiantObject.littlePropertyTwo }} + {{ bigGiantObject.littlePropertyThree }} +
` +}; +``` + +Component props should be passed explicitly, like this: + +``` +const BetterParentComponent = { + data: bigGiantObject, + template: ` + + `, +}; + +const BetterChildComponent = { + props: { + littlePropertyOne: { type: String }, + littlePropertyTwo: { type: String }, + }, + template: ` +
+ {{ littlePropertyOne }} {{ littlePropertyTwo }} +
` +}; +``` + +For deeper models, still try to encapsulate the data conservatively. An example +would look like this: + +``` +const DeepModelParentComponent = { + data: bigGiantObject, + template: ` + + `, +}; + +const DeepModelChildComponent = { + props: { + tierTwo: { type: Object }, + tierThree: { type: Object }, + }, + template: ` +
+ {{ tierTwo.littlePropertyOne }} {{ tierTwo: littlePropertyTwo }} +
+
+ {{ tierThree.littlePropertyOne }} {{ tierThree: littlePropertyTwo }} +
+ ` +}; +``` + +If the number of props you need to pass is very large or you need to pass a group of +properties down many layers, it may be simpler and more maintainable to just pass +a single object. If you're not sure, ask a Frontend Architecture expert. + + ## Style guide Please refer to the Vue section of our [style guide](style_guide_js.md#vuejs) -- cgit v1.2.3