Browse Source

Using 2-way subscription to signal change in remaning when todos changed

Herbert Vojčík 11 years ago
parent
commit
8401c61150
2 changed files with 7 additions and 2 deletions
  1. 6 2
      lib/js/Trapped-Demo.js
  2. 1 0
      lib/st/Trapped-Demo.st

+ 6 - 2
lib/js/Trapped-Demo.js

@@ -16,6 +16,10 @@ return smalltalk.withContext(function($ctx1) {
 smalltalk.App.superclass.fn.prototype._initialize.apply(_st(self), []);
 self._dispatcher_(_st($SimpleKeyedPubSub())._new());
 self._model_(_st(_st($AppModel())._new())._title_("Todo"));
+self._watch_do_([["todos"], nil],(function(){
+return smalltalk.withContext(function($ctx2) {
+return _st(self._dispatcher())._changed_([["remaining"]]);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
 _st((function(){
 return smalltalk.withContext(function($ctx2) {
 return self._modify_do_([["todos"]],(function(){
@@ -25,8 +29,8 @@ return [smalltalk.HashedCollection._from_(["text".__minus_gt("learn trapped"),"d
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}))._valueWithTimeout_((2000));
 return self}, function($ctx1) {$ctx1.fill(self,"initialize",{},smalltalk.App)})},
 args: [],
-source: "initialize\x0a\x09super initialize.\x0a    self dispatcher: SimpleKeyedPubSub new.\x0a    self model: (AppModel new title: 'Todo').\x0a    [ self modify: #((todos)) do: [{\x0a        #{'text'->'learn trapped'. 'done'->true}.\x0a        #{'text'->'build a trapped app'. 'done'->false}\x0a    }]] valueWithTimeout: 2000",
-messageSends: ["initialize", "dispatcher:", "new", "model:", "title:", "valueWithTimeout:", "modify:do:", "->"],
+source: "initialize\x0a\x09super initialize.\x0a    self dispatcher: SimpleKeyedPubSub new.\x0a    self model: (AppModel new title: 'Todo').\x0a\x09self watch: #((todos) nil) do: [ self dispatcher changed: #((remaining)) ].\x0a    [ self modify: #((todos)) do: [{\x0a        #{'text'->'learn trapped'. 'done'->true}.\x0a        #{'text'->'build a trapped app'. 'done'->false}\x0a    }]] valueWithTimeout: 2000",
+messageSends: ["initialize", "dispatcher:", "new", "model:", "title:", "watch:do:", "changed:", "dispatcher", "valueWithTimeout:", "modify:do:", "->"],
 referencedClasses: ["SimpleKeyedPubSub", "AppModel"]
 }),
 smalltalk.App);

+ 1 - 0
lib/st/Trapped-Demo.st

@@ -37,6 +37,7 @@ initialize
 	super initialize.
     self dispatcher: SimpleKeyedPubSub new.
     self model: (AppModel new title: 'Todo').
+	self watch: #((todos) nil) do: [ self dispatcher changed: #((remaining)) ].
     [ self modify: #((todos)) do: [{
         #{'text'->'learn trapped'. 'done'->true}.
         #{'text'->'build a trapped app'. 'done'->false}