Trapped-Todo.st 2.9 KB

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