1 | <!DOCTYPE html>
|
2 | <html>
|
3 | <head>
|
4 | <meta charset="utf-8">
|
5 | <link rel="stylesheet" href="assets/style.css?t=a44c9e9d">
|
6 | <link rel="stylesheet" href="http://ricostacruz.com/docpress-rsc/style.css">
|
7 | <script src="assets/script.js?t=2bcc4b59"></script>
|
8 | <title>Extensions example</title>
|
9 | <meta name="viewport" content="width=device-width">
|
10 | <script>
|
11 | if (false || window.location.hostname.indexOf("ricostacruz.com") === window.location.hostname.length - 15) {
|
12 | (function(d,o,c,p,r,e,s){d['GoogleAnalyticsObject']=r;d[r]=d[r]||function(){(d[r].q=d[r].q||[]).push(arguments)},d[r].l=1*new Date();e=o.createElement(c),s=o.getElementsByTagName(c)[0];e.async=1;e.src=p;s.parentNode.insertBefore(e,s)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
13 | ga('create',"UA-20473929-1",'auto')
|
14 | }
|
15 | </script>
|
16 | </head>
|
17 | <body class="-menu-visible">
|
18 | <div class="doc-layout">
|
19 | <div class="toggle menu-toggle js-menu-toggle"></div>
|
20 | <div class="body page-extensions_example">
|
21 | <div class="header-nav">
|
22 | <div class="right"><a href="https://github.com/rstacruz/scour" data-title="rstacruz/scour" class="iconlink">
|
23 | <span class="icon -github"></span></a>
|
24 | </div>
|
25 | </div>
|
26 | <div class="markdown-body"><h1 id="extensions-example">Extensions example</h1>
|
27 | <p>Here’s a simple data set which would be common with data that comes from SQL
|
28 | sources. Notice that it has foreign key relations in it: objects in <code>albums</code>
|
29 | have the <code>artist_id</code> property that maps to the ID’s in <code>artists</code>.</p>
|
30 | <h5 class="file-heading" id="sample-data">Sample data</h5>
|
31 | <pre><code class="lang-js">data =
|
32 | { artists:
|
33 | { <span class="hljs-number">1</span>: { id: <span class="hljs-number">1</span>, name: <span class="pl-s">'Ella Fitzgerald'</span> },
|
34 | <span class="hljs-number">2</span>: { id: <span class="hljs-number">2</span>, name: <span class="pl-s">'Frank Sinatra'</span> },
|
35 | <span class="hljs-number">3</span>: { id: <span class="hljs-number">3</span>, name: <span class="pl-s">'Miles Davis'</span> },
|
36 | <span class="hljs-number">4</span>: { id: <span class="hljs-number">4</span>, name: <span class="pl-s">'Taylor Swift'</span> } },
|
37 | albums:
|
38 | { <span class="hljs-number">1</span>: { id: <span class="hljs-number">1</span>, name: <span class="pl-s">'Kind of Blue'</span>, genre: <span class="pl-s">'Jazz'</span>, artist_id: <span class="hljs-number">3</span> },
|
39 | <span class="hljs-number">2</span>: { id: <span class="hljs-number">2</span>, name: <span class="pl-s">'Come Fly With Me'</span>, genre: <span class="pl-s">'Jazz'</span>, artist_id: <span class="hljs-number">2</span> },
|
40 | <span class="hljs-number">3</span>: { id: <span class="hljs-number">3</span>, name: <span class="pl-s">'1984'</span>, genre: <span class="pl-s">'Pop'</span>, artist_id: <span class="hljs-number">4</span> } } }
|
41 | </code></pre>
|
42 | <h2 id="defining-extensions">Defining extensions</h2>
|
43 | <p>You can define extensions to traverse this intelligently, like ActiveRecord.
|
44 | Here, we’ll use <a href="index.html#use">use()</a> to define extra methods that will
|
45 | be applied to certain keypaths.</p>
|
46 | <h5 class="file-heading" id="models">Models</h5>
|
47 | <pre><code class="lang-js">Root = {
|
48 | artists () { <span class="pl-k">return</span> <span class="pl-k">this</span>.go(<span class="pl-s">'artists'</span>) },
|
49 | albums () { <span class="pl-k">return</span> <span class="pl-k">this</span>.go(<span class="pl-s">'albums'</span>) }
|
50 | }
|
51 |
|
52 | Artist = {
|
53 | <span class="pl-c">// Defines a has-many relationship</span>
|
54 | albums () {
|
55 | <span class="pl-k">return</span> <span class="pl-k">this</span>.root.albums().filter({ artist_id: <span class="pl-k">this</span>.get(<span class="pl-s">'id'</span>) })
|
56 | }
|
57 | }
|
58 |
|
59 | Album = {
|
60 | <span class="pl-c">// Defines a belongs-to relationship</span>
|
61 | artist () {
|
62 | <span class="pl-k">return</span> <span class="pl-k">this</span>.root.artists().go(<span class="pl-k">this</span>.get(<span class="pl-s">'artist_id'</span>))
|
63 | }
|
64 | }
|
65 | </code></pre>
|
66 | <h5 class="file-heading" id="using-with-scour">Using with scour</h5>
|
67 | <pre><code class="lang-js">db = scour(data)
|
68 | .use({
|
69 | <span class="pl-s">''</span>: Root,
|
70 | <span class="pl-s">'artists.*'</span>: Artist,
|
71 | <span class="pl-s">'albums.*'</span>: Album
|
72 | })
|
73 | </code></pre>
|
74 | <h2 id="root-extensions">Root extensions</h2>
|
75 | <p>With the extensions to the root (<code>''</code>), it defined an alias to <code>go('albums')</code>
|
76 | as <code>albums()</code>.</p>
|
77 | <pre><code class="lang-js">albums = db.albums().filter({ genre: <span class="pl-s">'Jazz'</span> })
|
78 | </code></pre>
|
79 | <h2 id="traversing-relationships">Traversing relationships</h2>
|
80 | <p>Combined with the extensions to <code>artists.*</code> and <code>albums.*</code>, we can use it to
|
81 | traverse back-and-forth along “models”.</p>
|
82 | <h5 class="file-heading" id="chaining-example">Chaining example</h5>
|
83 | <pre><code class="lang-js">db
|
84 | .artists()
|
85 | .find({ name: <span class="pl-s">'Taylor Swift'</span> })
|
86 | .albums()
|
87 | </code></pre>
|
88 | <h5 class="file-heading" id="another-example">Another example</h5>
|
89 | <pre><code class="lang-js">albums.each((album) => {
|
90 | <span class="pl-c1">console</span>.log(album.artist().get(<span class="pl-s">'name'</span>))
|
91 | })
|
92 | </code></pre>
|
93 |
|
94 | </div>
|
95 | <div class="footer-nav">
|
96 | <div class="left"><a href="comparison.html"><span class="title">Comparison with others</span></a></div>
|
97 | <div class="right"><a href="redux_example.html"><span class="label">Next: </span><span class="title">Redux example</span></a></div>
|
98 | </div>
|
99 | </div>
|
100 | <div class="menu toc-menu">
|
101 | <li class="menu-item -level-0 -parent">
|
102 | <ul class="submenu">
|
103 | <li class="menu-item -level-1"><a href="index.html" class="link title link-index">scour.js</a>
|
104 | </li>
|
105 | <li class="menu-item -level-1 -parent"><span class="title">Appendix</span>
|
106 | <ul class="submenu">
|
107 | <li class="menu-item -level-2"><a href="HISTORY.html" class="link title link-history">Changelog</a>
|
108 | </li>
|
109 | <li class="menu-item -level-2"><a href="comparison.html" class="link title link-comparison">Comparison with others</a>
|
110 | </li>
|
111 | <li class="menu-item -level-2"><a href="extensions_example.html" class="link title -active link-extensions_example">Extensions example</a>
|
112 | <ul class="headings heading-list">
|
113 | <li class="heading-item -depth-2"><a href="#defining-extensions" class="hlink link-defining-extensions">Defining extensions</a>
|
114 | </li>
|
115 | <li class="heading-item -depth-2"><a href="#root-extensions" class="hlink link-root-extensions">Root extensions</a>
|
116 | </li>
|
117 | <li class="heading-item -depth-2"><a href="#traversing-relationships" class="hlink link-traversing-relationships">Traversing relationships</a>
|
118 | </li>
|
119 | </ul>
|
120 | </li>
|
121 | <li class="menu-item -level-2"><a href="redux_example.html" class="link title link-redux_example">Redux example</a>
|
122 | </li>
|
123 | </ul>
|
124 | </li>
|
125 | </ul>
|
126 | </li>
|
127 | </div>
|
128 | </div>
|
129 | </body>
|
130 | </html> |
\ | No newline at end of file |