Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Foo</title>
  5. <meta charset='utf-8' />
  6. <meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' />
  7. <style type='text/css'>
  8. div {
  9. cursor:pointer;
  10. cursor:hand;
  11. position:absolute;
  12. top:0;
  13. left:0;
  14. }
  15.  
  16. </style>
  17. <script type='text/javascript'>
  18. window.onload = function() {
  19. var s = document.getElementsByTagName('div'), cur = 0, ti;
  20. if (!s) return;
  21. function go(n) {
  22. cur = n;
  23. var i = 1e3, e = s[n], t;
  24. document.body.className = e.dataset.bodyclass || '';
  25. for (var k = 0; k < s.length; k++) s[k].style.display = 'none';
  26. e.style.display = 'inline';
  27. e.style.fontSize = i + 'px';
  28. if (e.firstChild && e.firstChild.nodeName === 'IMG') {
  29. document.body.style.backgroundImage = 'url(' + e.firstChild.src + ')';
  30. e.firstChild.style.display = 'none';
  31. if ('classList' in e) e.classList.add('imageText');
  32. } else {
  33. document.body.style.backgroundImage = '';
  34. document.body.style.backgroundColor = e.style.backgroundColor;
  35. }
  36. if (ti !== undefined) window.clearInterval(ti);
  37. t = parseInt(e.dataset.timeToNext || 0, 10);
  38. if (t > 0) ti = window.setTimeout(fwd, (t * 1000));
  39. while (
  40. e.offsetWidth > window.innerWidth ||
  41. e.offsetHeight > window.innerHeight) {
  42. e.style.fontSize = (i -= 2) + 'px';
  43. if (i < 0) break;
  44. }
  45. e.style.marginTop = ((window.innerHeight - e.offsetHeight) / 2) + 'px';
  46. if (window.location.hash !== n) window.location.hash = n;
  47. document.title = e.textContent || e.innerText;
  48. }
  49. document.onclick = function() { go(++cur % (s.length)); };
  50. function fwd() { go(Math.min(s.length - 1, ++cur)); }
  51. function rev() { go(Math.max(0, --cur)); }
  52. document.onkeydown = function(e) {
  53. if (e.which === 39 || e.which === 34 || e.which === 40) fwd();
  54. if (e.which === 37 || e.which === 33 || e.which === 38) rev();
  55. };
  56. document.ontouchstart = function(e) {
  57. var x0 = e.changedTouches[0].pageX;
  58. document.ontouchend = function(e) {
  59. var x1 = e.changedTouches[0].pageX;
  60. if (x1 - x0 < 0) fwd();
  61. if (x1 - x0 > 0) rev();
  62. };
  63. };
  64. function parse_hash() {
  65. return Math.max(Math.min(
  66. s.length - 1,
  67. parseInt(window.location.hash.substring(1), 10)), 0);
  68. }
  69. if (window.location.hash) cur = parse_hash() || cur;
  70. window.onhashchange = function() {
  71. var c = parse_hash();
  72. if (c !== cur) go(c);
  73. };
  74. go(cur);
  75. };
  76. </script></head><body>
  77. <div><h2 id="build-fe-apps-the-mac-way">build FE apps the MAC way</h2>
  78. <h4 id="model-action-component-">model, action, component*</h4>
  79. <h5 id="-i-just-made-that-up">* i just made that up</h5>
  80. </div>
  81. <div><p><em>M</em>odels = API</p>
  82. </div>
  83. <div><p><em>A</em>ctions = Flux</p>
  84. </div>
  85. <div><p><em>C</em>omponents = React Views</p>
  86. </div>
  87. <div><p>Why is this different?</p>
  88. </div>
  89. <div><h2 id="mvc-or-mvvm-">MVC (or MVVM):</h2>
  90. <ul>
  91. <li>view code in separate files (incl. partials)</li>
  92. <li>logic in views, too, at times</li>
  93. <li>fat models</li>
  94. <li>controllers* tend to contain event code</li>
  95. </ul>
  96. </div>
  97. <div><h2 id="mac-flux-react-">MAC (Flux + React):</h2>
  98. <ul>
  99. <li>view and model code are in same place (<em>c</em>omponents)</li>
  100. <li>events are <em>separate</em>, <em>one-way</em>, and <em>queueable</em></li>
  101. <li>server (request-driven) and UI (user-driven) events are in one place</li>
  102. </ul>
  103. </div>
  104. <div><h1 id="actions">Actions</h1>
  105. <ul>
  106. <li>Dispatched by, shockingly, a dispatcher</li>
  107. <li>(usually EventEmitter subclass)</li>
  108. </ul>
  109. </div>
  110. <div><h1 id="components">Components</h1>
  111. <ul>
  112. <li>View code in <code>render()</code></li>
  113. <li>props as <code>this.props</code> (typesafe)</li>
  114. <li>view logic in local functions</li>
  115. </ul>
  116. </div>
  117. <div><h1 id="models">Models</h1>
  118. <ul>
  119. <li>Event driven too!</li>
  120. <li>Loading locally? <code>POSTS_LOADED_EVENT</code></li>
  121. <li>Loading via JSON request? <code>POSTS_LOADED_EVENT</code></li>
  122. </ul>
  123. </div>
  124. <div><h2 id="for-example-post-component-">For example - Post Component:</h2>
  125. <pre><code class="lang-javascript">Post = React.createClass({
  126. render: function() {
  127. return <p className="post"></p>;
  128. }
  129. });
  130. </code></pre>
  131. </div>
  132. <div><h2 id="blank-post-logic">Blank post logic</h2>
  133. <pre><code class="lang-javascript">...
  134. body: function() {
  135. if (this.text && this.text.length > 0) {
  136. return this.text;
  137. } else {
  138. return "No content in this post.";
  139. }
  140. },
  141. render: function() {
  142. return (
  143. <p className="post">
  144. {this.body()}
  145. </p>
  146. );
  147. }
  148. ...
  149. </code></pre>
  150. </div>
  151. <div><h3 id="one-more-piece-">One more piece!</h3>
  152. <h1 id="stores">Stores</h1>
  153. </div>
  154. <div><h2 id="poststore">PostStore</h2>
  155. <pre><code class="lang-javascript">// PostStore.js
  156. var _posts = [];
  157. var PostStore = assign({}, EventEmitter.prototype, {
  158. emitChange: function() {
  159. this.emit(CHANGE_EVENT);
  160. },
  161. addChangeListener: function(callback) {
  162. this.on(CHANGE_EVENT, callback);
  163. },
  164. removeChangeListener: function(callback) {
  165. this.removeListener(CHANGE_EVENT, callback);
  166. }
  167. });
  168. ...
  169. </code></pre>
  170. </div>
  171. <div><h2 id="poststore">PostStore</h2>
  172. <pre><code class="lang-javascript">// Farther down in PostStore.js
  173.  
  174. var token = PostStore.dispatchToken;
  175. token = Dispatcher.register(function(action) {
  176. switch (action.type) {
  177. // ...
  178. case ActionTypes.POST_CHANGED:
  179. PostStore.updatePost(action.post);
  180. Dispatcher.waitFor([OtherStore.dispatchToken]);
  181. PostStore.emitChange();
  182. break;
  183. }
  184. });
  185. </code></pre>
  186. </div>
  187. <div><h1 id="in-summary">In Summary</h1>
  188. <p>UI/Requests <em>create</em> Actions <em>in</em> Dispatcher, <em>which calls</em> Stores. Stores <em>trigger</em> Components.</p>
  189. </div>
  190. <div><h1 id="how-to-test">How to Test</h1>
  191. <p>Just add synthetic events to your dispatcher. Then, unit test all your Stores, Components, etc.</p>
  192. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement