Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. // To avoid passing params/props through out the object graph construction
  2. // You separate your construction logic from the business logic of your components
  3. // And initialise your graph using factory methods close to the application bootstrap
  4.  
  5. import util from './util';
  6.  
  7. class App {
  8. constructor(page) {
  9. this.page = page
  10. }
  11.  
  12. // Object graph construction is separate from business logic
  13. // Note that dependencies don't have to be passed through the entire chain
  14. static create() {
  15. const atom = new Atom(util);
  16. const molecule = new Molecule(atom);
  17. const organism = new Organism(molecule);
  18. const template = new Template(organism);
  19. const page = new Page(template);
  20. return new App(page);
  21. }
  22. }
  23.  
  24. // AKA in JSX
  25. class App {
  26. create() {
  27. const atom = <Atom util={util}/>;
  28. const molecule = <Molecule atom={atom}/>;
  29. const organism = <Organism molecule={molecule}/>;
  30. const template = <Template organism={organism}/>;
  31. const page = <Page template={template}/>;
  32. const App = () => <App page={page}/>;
  33. return <App/>
  34. }
  35. }
  36.  
  37. // If object graph construction is mixed with business/component logic
  38. // then the params/props must be passed through the entire chain of construction
  39. import util from './util';
  40.  
  41. class App {
  42. // Object graph construction is mixed with business logic
  43. constructor() {
  44. this.page = new Page(util);
  45. }
  46. }
  47. class Page {
  48. constructor(util) {
  49. this.template = new Template(util);
  50. }
  51. }
  52. class Template {
  53. constructor(util) {
  54. this.organism = new Organism(util);
  55. }
  56. }
  57. class Organism {
  58. constructor(util) {
  59. this.molecule = new Molecule(util);
  60. }
  61. }
  62. class Molecule {
  63. constructor(util) {
  64. this.atom = new Atom(util);
  65. }
  66. }
  67. class Atom {
  68. constructor(util) {
  69. this.util = util;
  70. }
  71. }
  72.  
  73. // In JSX
  74. const App = (util) => {
  75. const Page = (util) => {
  76. const Template = (util) => {
  77. const Organism = (util) => {
  78. const Molecule = (util) => {
  79. const Atom = (util) => {
  80. const fooBar = util.doTheThing();
  81. return <div>{fooBar}</div>
  82. };
  83.  
  84. return <Atom util={util}/>;
  85. };
  86.  
  87. return <Molecule util={util}/>;
  88. };
  89.  
  90. return <Organism util={util}/>;
  91. };
  92.  
  93. return <Template util={util}/>;
  94. };
  95.  
  96. return <Page util={util}/>;
  97. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement