Trapped-Todo.st 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. Smalltalk createPackage: 'Trapped-Todo'!
  2. IsolatingTrapper subclass: #App
  3. instanceVariableNames: ''
  4. package: 'Trapped-Todo'!
  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 axon: SimpleAxon new.
  34. self model: (AppModel new title: 'Todo').
  35. self axon addInterest: (
  36. InterestedInTrapPathSubtree new
  37. aspect: #((todos))
  38. block: [ self changed: #((remaining)) ]).
  39. [ self modify: #((todos)) do: [{
  40. #{'text'->'learn trapped'. 'done'->true}.
  41. #{'text'->'build a trapped app'. 'done'->false}
  42. }]] valueWithTimeout: 2000
  43. ! !
  44. Object subclass: #AppModel
  45. instanceVariableNames: 'title todos todoText'
  46. package: 'Trapped-Todo'!
  47. !AppModel commentStamp!
  48. // Code from AngularJS Todo example, http://angularjs.org/#todo-js
  49. function TodoCtrl($scope) {
  50. $scope.todos = [
  51. {text:'learn angular', done:true},
  52. {text:'build an angular app', done:false}];
  53. $scope.addTodo = function() {
  54. $scope.todos.push({text:$scope.todoText, done:false});
  55. $scope.todoText = '';
  56. };
  57. $scope.remaining = function() {
  58. var count = 0;
  59. angular.forEach($scope.todos, function(todo) {
  60. count += todo.done ? 0 : 1;
  61. });
  62. return count;
  63. };
  64. $scope.archive = function() {
  65. var oldTodos = $scope.todos;
  66. $scope.todos = [];
  67. angular.forEach(oldTodos, function(todo) {
  68. if (!!todo.done) $scope.todos.push(todo);
  69. });
  70. };
  71. }!
  72. !AppModel methodsFor: 'accessing'!
  73. remaining
  74. ^self todosNotDone size
  75. !
  76. title
  77. ^title
  78. !
  79. title: aString
  80. title := aString
  81. !
  82. todoText
  83. ^todoText
  84. !
  85. todoText: aString
  86. todoText := aString
  87. !
  88. todos
  89. ^todos
  90. !
  91. todos: anArray
  92. todos := anArray
  93. !
  94. todosNotDone
  95. ^self todos reject: [ :each | each at: 'done' ]
  96. ! !
  97. !AppModel methodsFor: 'action'!
  98. addTodo
  99. self todos add: #{'text'->self todoText. 'done'->false}.
  100. self todoText: ''
  101. !
  102. archive
  103. self todos: self todosNotDone
  104. ! !
  105. !TrappedProcessor class methodsFor: '*Trapped-Todo'!
  106. classDoneXxx
  107. "This processor is not used any more,
  108. it was replaced by generic
  109. (replace ^ with ^done-) (attr class)
  110. in HTML.
  111. This example is left here to show how you can create quick
  112. toView-only processor without class by just passing a block"
  113. ^self dataToView: [ :carrier | carrier target class: 'done-', carrier value ]
  114. ! !