Trapped-Demo.st 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. Smalltalk current createPackage: 'Trapped-Demo' properties: #{}!
  2. TrappedMWIsolated subclass: #App
  3. instanceVariableNames: ''
  4. package: 'Trapped-Demo'!
  5. !App commentStamp!
  6. // Code from AngularJS Todo example, http://angularjs.org/#todo-js
  7. function TodoCtrl($scope) {
  8. $scope.todos = [
  9. {text:'learn angular', done:true},
  10. {text:'build an angular app', done:false}];
  11. $scope.addTodo = function() {
  12. $scope.todos.push({text:$scope.todoText, done:false});
  13. $scope.todoText = '';
  14. };
  15. $scope.remaining = function() {
  16. var count = 0;
  17. angular.forEach($scope.todos, function(todo) {
  18. count += todo.done ? 0 : 1;
  19. });
  20. return count;
  21. };
  22. $scope.archive = function() {
  23. var oldTodos = $scope.todos;
  24. $scope.todos = [];
  25. angular.forEach(oldTodos, function(todo) {
  26. if (!!todo.done) $scope.todos.push(todo);
  27. });
  28. };
  29. }!
  30. !App methodsFor: 'initialization'!
  31. initialize
  32. super initialize.
  33. self dispatcher: TrappedDumbDispatcher new.
  34. self model: (AppModel new title: 'Todo').
  35. [ self modify: #(#todos) do: [{
  36. #{'text'->'learn trapped'. 'done'->true}.
  37. #{'text'->'build a trapped app'. 'done'->false}
  38. }]] valueWithTimeout: 2000
  39. ! !
  40. Object subclass: #AppModel
  41. instanceVariableNames: 'title todos todoText'
  42. package: 'Trapped-Demo'!
  43. !AppModel commentStamp!
  44. // Code from AngularJS Todo example, http://angularjs.org/#todo-js
  45. function TodoCtrl($scope) {
  46. $scope.todos = [
  47. {text:'learn angular', done:true},
  48. {text:'build an angular app', done:false}];
  49. $scope.addTodo = function() {
  50. $scope.todos.push({text:$scope.todoText, done:false});
  51. $scope.todoText = '';
  52. };
  53. $scope.remaining = function() {
  54. var count = 0;
  55. angular.forEach($scope.todos, function(todo) {
  56. count += todo.done ? 0 : 1;
  57. });
  58. return count;
  59. };
  60. $scope.archive = function() {
  61. var oldTodos = $scope.todos;
  62. $scope.todos = [];
  63. angular.forEach(oldTodos, function(todo) {
  64. if (!!todo.done) $scope.todos.push(todo);
  65. });
  66. };
  67. }!
  68. !AppModel methodsFor: 'accessing'!
  69. remaining
  70. ^(self todos select: [ :each | each at: 'done' ]) size
  71. !
  72. title
  73. ^title
  74. !
  75. title: aString
  76. title := aString
  77. !
  78. todoText
  79. ^todoText
  80. !
  81. todoText: aString
  82. todoText := aString
  83. !
  84. todos
  85. ^todos
  86. !
  87. todos: anArray
  88. todos := anArray
  89. ! !
  90. !AppModel methodsFor: 'action'!
  91. addTodo
  92. self todos add: #{'text'->self todoText. 'done'->false}.
  93. self todoText: ''
  94. !
  95. archive
  96. self todos: (self todos reject: [ :each | each at: 'done' ])
  97. ! !
  98. Widget subclass: #AppView
  99. instanceVariableNames: ''
  100. package: 'Trapped-Demo'!
  101. !AppView commentStamp!
  102. <!!-- Code from AngularJS Todo example, http://angularjs.org/#todo-html -->
  103. <body>
  104. <h2>Todo</h2>
  105. <div ng-controller="TodoCtrl">
  106. <span>{{remaining()}} of {{todos.length}} remaining</span>
  107. [ <a href="" ng-click="archive()">archive</a> ]
  108. <ul class="unstyled">
  109. <li ng-repeat="todo in todos">
  110. <input type="checkbox" ng-model="todo.done">
  111. <span class="done-{{todo.done}}">{{todo.text}}</span>
  112. </li>
  113. </ul>
  114. <form ng-submit="addTodo()">
  115. <input type="text" ng-model="todoText" size="30"
  116. placeholder="add new todo here">
  117. <input class="btn-primary" type="submit" value="add">
  118. </form>
  119. </div>
  120. </body>!
  121. !AppView methodsFor: 'rendering'!
  122. renderOn: html
  123. | snap |
  124. html h2 trap: #(#title).
  125. snap := Trapped current snapshot.
  126. html div trap: #(#todos) toggle: [ snap do: [
  127. html span trap:#(#remaining).
  128. html with: ' of '.
  129. html span trap: #(#todos #size).
  130. html with: ' remaining [ '.
  131. html a href:''; onClick: [[
  132. snap model modify: snap path allButFirst do: [ :model | model archive ].
  133. false
  134. ] value "amber GH-314 workaround"]; with: 'archive'.
  135. html with: ' ]'.
  136. html ul trapIter: #(#todos) tag: #li do: [ :each |
  137. html root empty.
  138. html input type: 'checkbox'; trap: #('done').
  139. html span trap: #('done') read: [ :model | html root class: 'done-', model ]; trap: #('text').
  140. ].
  141. html form onSubmit: [[
  142. snap model modify: snap path allButFirst do: [ :model | model addTodo ].
  143. false
  144. ] value "amber GH-314 workaround"]; with: [
  145. html input type: 'text'; trap: #(#todoText); at: 'size' put: 30; placeholder: 'add new todo here'.
  146. html input class: 'btn-primary'; type: 'submit'; value: 'add'.
  147. ].
  148. ]] ifNotPresent: [ html with: 'Loading ...' ]
  149. ! !
  150. TrappedDispatcher subclass: #TrappedDumbDispatcher
  151. instanceVariableNames: 'queue'
  152. package: 'Trapped-Demo'!
  153. !TrappedDumbDispatcher methodsFor: 'accessing'!
  154. add: aSubscription
  155. queue add: aSubscription.
  156. ! !
  157. !TrappedDumbDispatcher methodsFor: 'bookkeeping'!
  158. clean
  159. queue := queue select: [ :each | each isEnabled ]
  160. ! !
  161. !TrappedDumbDispatcher methodsFor: 'enumeration'!
  162. do: aBlock
  163. queue do: aBlock
  164. ! !
  165. !TrappedDumbDispatcher methodsFor: 'initialization'!
  166. initialize
  167. queue := OrderedCollection new
  168. ! !