Trapped-Todo.st 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. Smalltalk createPackage: 'Trapped-Todo'!
  2. (Smalltalk packageAt: 'Trapped-Todo' ifAbsent: [ self error: 'Package not created: Trapped-Todo' ]) imports: {'trapped/Trapped-Processors'}!
  3. Object subclass: #TodoApp
  4. instanceVariableNames: ''
  5. package: 'Trapped-Todo'!
  6. !TodoApp methodsFor: 'startup'!
  7. start
  8. | viewModel axon |
  9. viewModel := Axolator on: TrappedTodo new.
  10. axon := SimpleAxon new.
  11. axon addInterest: (TrappedPosition
  12. interestOn: #((todos) nil)
  13. block: [ axon changed: #((remaining)) ]).
  14. [ viewModel axes: #((todos)) transform: [{
  15. #{'text'->'learn trapped'. 'done'->true}.
  16. #{'text'->'build a trapped app'. 'done'->false}
  17. }]] valueWithTimeout: 2000.
  18. viewModel axxord: axon.
  19. Trapped start: { viewModel }
  20. ! !
  21. !TodoApp class methodsFor: 'startup'!
  22. start
  23. ^ self new start
  24. ! !
  25. Object subclass: #TrappedTodo
  26. instanceVariableNames: 'title todos todoText'
  27. package: 'Trapped-Todo'!
  28. !TrappedTodo commentStamp!
  29. // Code from AngularJS Todo example, http://angularjs.org/#todo-js
  30. function TodoCtrl($scope) {
  31. $scope.todos = [
  32. {text:'learn angular', done:true},
  33. {text:'build an angular app', done:false}];
  34. $scope.addTodo = function() {
  35. $scope.todos.push({text:$scope.todoText, done:false});
  36. $scope.todoText = '';
  37. };
  38. $scope.remaining = function() {
  39. var count = 0;
  40. angular.forEach($scope.todos, function(todo) {
  41. count += todo.done ? 0 : 1;
  42. });
  43. return count;
  44. };
  45. $scope.archive = function() {
  46. var oldTodos = $scope.todos;
  47. $scope.todos = [];
  48. angular.forEach(oldTodos, function(todo) {
  49. if (!!todo.done) $scope.todos.push(todo);
  50. });
  51. };
  52. }!
  53. !TrappedTodo methodsFor: 'accessing'!
  54. remaining
  55. ^self todosNotDone size
  56. !
  57. title
  58. ^title
  59. !
  60. title: aString
  61. title := aString
  62. !
  63. todoText
  64. ^todoText
  65. !
  66. todoText: aString
  67. todoText := aString
  68. !
  69. todos
  70. ^todos
  71. !
  72. todos: anArray
  73. todos := anArray
  74. !
  75. todosNotDone
  76. ^self todos reject: [ :each | each at: 'done' ]
  77. ! !
  78. !TrappedTodo methodsFor: 'action'!
  79. addTodo
  80. self todos add: #{'text'->self todoText. 'done'->false}.
  81. self todoText: ''
  82. !
  83. archive
  84. self todos: self todosNotDone
  85. ! !
  86. !TrappedTodo methodsFor: 'initialization'!
  87. initialize
  88. super initialize.
  89. title := 'Todo'.
  90. todoText := nil.
  91. todos := nil
  92. ! !
  93. !TrappedProcessor class methodsFor: '*Trapped-Todo'!
  94. classDoneXxx
  95. "This processor is not used any more,
  96. it was replaced by generic
  97. (replace ^ with ^done-) (attr class)
  98. in HTML.
  99. This example is left here to show how you can create quick
  100. toView-only processor without class by just passing a block"
  101. ^self dataToView: [ :carrier | carrier target class: 'done-', carrier value ]
  102. ! !