Browse Source

TrappedPathStack; TrappedFly not singleton.

Herbert Vojčík 12 years ago
parent
commit
743e010c69
3 changed files with 352 additions and 63 deletions
  1. 116 18
      js/Trapped-Frontend.deploy.js
  2. 169 31
      js/Trapped-Frontend.js
  3. 67 14
      st/Trapped-Frontend.st

+ 116 - 18
js/Trapped-Frontend.deploy.js

@@ -1,4 +1,97 @@
 smalltalk.addPackage('Trapped-Frontend', {});
+smalltalk.addClass('TrappedFly', smalltalk.Object, [], 'Trapped-Frontend');
+smalltalk.addMethod(
+"_name",
+smalltalk.method({
+selector: "name",
+fn: function (){
+var self=this;
+var $1;
+$1=smalltalk.send(smalltalk.send(self,"_class",[]),"_name",[]);
+return $1;
+}
+}),
+smalltalk.TrappedFly);
+
+smalltalk.addMethod(
+"_start",
+smalltalk.method({
+selector: "start",
+fn: function (){
+var self=this;
+smalltalk.send(smalltalk.send((smalltalk.Trapped || Trapped),"_current",[]),"_register_name_",[self,smalltalk.send(self,"_name",[])]);
+return self}
+}),
+smalltalk.TrappedFly);
+
+
+smalltalk.addMethod(
+"_start",
+smalltalk.method({
+selector: "start",
+fn: function (){
+var self=this;
+smalltalk.send(smalltalk.send(self,"_new",[]),"_start",[]);
+return self}
+}),
+smalltalk.TrappedFly.klass);
+
+
+smalltalk.addClass('TrappedPathStack', smalltalk.Object, ['elements'], 'Trapped-Frontend');
+smalltalk.addMethod(
+"_append_",
+smalltalk.method({
+selector: "append:",
+fn: function (anArray){
+var self=this;
+self["@elements"]=smalltalk.send(self["@elements"],"__comma",[anArray]);
+return self}
+}),
+smalltalk.TrappedPathStack);
+
+smalltalk.addMethod(
+"_elements",
+smalltalk.method({
+selector: "elements",
+fn: function (){
+var self=this;
+return self["@elements"];
+}
+}),
+smalltalk.TrappedPathStack);
+
+smalltalk.addMethod(
+"_initialize",
+smalltalk.method({
+selector: "initialize",
+fn: function (){
+var self=this;
+self["@elements"]=[];
+return self}
+}),
+smalltalk.TrappedPathStack);
+
+smalltalk.addMethod(
+"_with_do_",
+smalltalk.method({
+selector: "with:do:",
+fn: function (anArray,aBlock){
+var self=this;
+var old;
+old=self["@elements"];
+smalltalk.send((function(){
+smalltalk.send(self,"_append_",[anArray]);
+return smalltalk.send(aBlock,"_value",[]);
+}),"_ensure_",[(function(){
+self["@elements"]=old;
+return self["@elements"];
+})]);
+return self}
+}),
+smalltalk.TrappedPathStack);
+
+
+
 smalltalk.addClass('TrappedSingleton', smalltalk.Object, [], 'Trapped-Frontend');
 smalltalk.addMethod(
 "_start",
@@ -94,8 +187,6 @@ var jq;
 var viewName;
 var modelName;
 var tokens;
-var model;
-var view;
 jq=smalltalk.send(elem,"_asJQuery",[]);
 jq;
 trap=smalltalk.send(jq,"_attr_",["data-trap"]);
@@ -106,41 +197,48 @@ viewName=smalltalk.send(tokens,"_first",[]);
 viewName;
 modelName=smalltalk.send(tokens,"_second",[]);
 modelName;
-model=smalltalk.send(smalltalk.send((smalltalk.Trapped || Trapped),"_current",[]),"_byName_",[modelName]);
-model;
+return smalltalk.send((smalltalk.Trapped || Trapped),"_with_do_",[[modelName],(function(){
 return smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send((smalltalk.Smalltalk || Smalltalk),"_current",[]),"_at_",[viewName]),"_new",[]),"_appendToJQuery_",[jq]);
 })]);
+})]);
 return self}
 }),
 smalltalk.Trapped);
 
 
+smalltalk.Trapped.klass.iVarNames = ['path'];
+smalltalk.addMethod(
+"_initialize",
+smalltalk.method({
+selector: "initialize",
+fn: function (){
+var self=this;
+self["@path"]=smalltalk.send((smalltalk.TrappedPathStack || TrappedPathStack),"_new",[]);
+return self}
+}),
+smalltalk.Trapped.klass);
 
-smalltalk.addClass('TrappedFly', smalltalk.TrappedSingleton, [], 'Trapped-Frontend');
 smalltalk.addMethod(
-"_name",
+"_path",
 smalltalk.method({
-selector: "name",
+selector: "path",
 fn: function (){
 var self=this;
-var $1;
-$1=smalltalk.send(smalltalk.send(self,"_class",[]),"_name",[]);
-return $1;
+return self["@path"];
 }
 }),
-smalltalk.TrappedFly);
+smalltalk.Trapped.klass);
 
 smalltalk.addMethod(
-"_start",
+"_with_do_",
 smalltalk.method({
-selector: "start",
-fn: function (){
+selector: "with:do:",
+fn: function (anArray,aBlock){
 var self=this;
-smalltalk.send(smalltalk.send((smalltalk.Trapped || Trapped),"_current",[]),"_register_name_",[self,smalltalk.send(self,"_name",[])]);
+smalltalk.send(self["@path"],"_with_do_",[anArray,aBlock]);
 return self}
 }),
-smalltalk.TrappedFly);
-
+smalltalk.Trapped.klass);
 
 
 smalltalk.addClass('TrappedView', smalltalk.Widget, [], 'Trapped-Frontend');
@@ -151,7 +249,7 @@ selector: "renderOn:",
 fn: function (html){
 var self=this;
 smalltalk.send(smalltalk.send(html,"_root",[]),"_empty",[]);
-smalltalk.send(html,"_with_",[smalltalk.send(smalltalk.send(smalltalk.send(self,"_class",[]),"_name",[]),"__comma",[": contents"])]);
+smalltalk.send(html,"_with_",[smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(self,"_class",[]),"_name",[]),"__comma",[": "]),"__comma",[smalltalk.send(smalltalk.send((smalltalk.Trapped || Trapped),"_path",[]),"_elements",[])])]);
 return self}
 }),
 smalltalk.TrappedView);

+ 169 - 31
js/Trapped-Frontend.js

@@ -1,4 +1,132 @@
 smalltalk.addPackage('Trapped-Frontend', {});
+smalltalk.addClass('TrappedFly', smalltalk.Object, [], 'Trapped-Frontend');
+smalltalk.addMethod(
+"_name",
+smalltalk.method({
+selector: "name",
+category: 'action',
+fn: function (){
+var self=this;
+var $1;
+$1=smalltalk.send(smalltalk.send(self,"_class",[]),"_name",[]);
+return $1;
+},
+args: [],
+source: "name\x0a\x09^ self class name",
+messageSends: ["name", "class"],
+referencedClasses: []
+}),
+smalltalk.TrappedFly);
+
+smalltalk.addMethod(
+"_start",
+smalltalk.method({
+selector: "start",
+category: 'action',
+fn: function (){
+var self=this;
+smalltalk.send(smalltalk.send((smalltalk.Trapped || Trapped),"_current",[]),"_register_name_",[self,smalltalk.send(self,"_name",[])]);
+return self},
+args: [],
+source: "start\x0a\x09Trapped current register: self name: self name",
+messageSends: ["register:name:", "name", "current"],
+referencedClasses: ["Trapped"]
+}),
+smalltalk.TrappedFly);
+
+
+smalltalk.addMethod(
+"_start",
+smalltalk.method({
+selector: "start",
+category: 'action',
+fn: function (){
+var self=this;
+smalltalk.send(smalltalk.send(self,"_new",[]),"_start",[]);
+return self},
+args: [],
+source: "start\x0a\x09self new start",
+messageSends: ["start", "new"],
+referencedClasses: []
+}),
+smalltalk.TrappedFly.klass);
+
+
+smalltalk.addClass('TrappedPathStack', smalltalk.Object, ['elements'], 'Trapped-Frontend');
+smalltalk.addMethod(
+"_append_",
+smalltalk.method({
+selector: "append:",
+category: 'descending',
+fn: function (anArray){
+var self=this;
+self["@elements"]=smalltalk.send(self["@elements"],"__comma",[anArray]);
+return self},
+args: ["anArray"],
+source: "append: anArray\x0a    elements := elements, anArray",
+messageSends: [","],
+referencedClasses: []
+}),
+smalltalk.TrappedPathStack);
+
+smalltalk.addMethod(
+"_elements",
+smalltalk.method({
+selector: "elements",
+category: 'accessing',
+fn: function (){
+var self=this;
+return self["@elements"];
+},
+args: [],
+source: "elements\x0a\x09^elements",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.TrappedPathStack);
+
+smalltalk.addMethod(
+"_initialize",
+smalltalk.method({
+selector: "initialize",
+category: 'initializing',
+fn: function (){
+var self=this;
+self["@elements"]=[];
+return self},
+args: [],
+source: "initialize\x0a\x09elements := #().",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.TrappedPathStack);
+
+smalltalk.addMethod(
+"_with_do_",
+smalltalk.method({
+selector: "with:do:",
+category: 'descending',
+fn: function (anArray,aBlock){
+var self=this;
+var old;
+old=self["@elements"];
+smalltalk.send((function(){
+smalltalk.send(self,"_append_",[anArray]);
+return smalltalk.send(aBlock,"_value",[]);
+}),"_ensure_",[(function(){
+self["@elements"]=old;
+return self["@elements"];
+})]);
+return self},
+args: ["anArray", "aBlock"],
+source: "with: anArray do: aBlock\x0a\x09| old |\x0a    old := elements.\x0a    [ self append: anArray.\x0a\x09aBlock value ] ensure: [ elements := old ]",
+messageSends: ["ensure:", "append:", "value"],
+referencedClasses: []
+}),
+smalltalk.TrappedPathStack);
+
+
+
 smalltalk.addClass('TrappedSingleton', smalltalk.Object, [], 'Trapped-Frontend');
 smalltalk.addMethod(
 "_start",
@@ -125,8 +253,6 @@ var jq;
 var viewName;
 var modelName;
 var tokens;
-var model;
-var view;
 jq=smalltalk.send(elem,"_asJQuery",[]);
 jq;
 trap=smalltalk.send(jq,"_attr_",["data-trap"]);
@@ -137,55 +263,67 @@ viewName=smalltalk.send(tokens,"_first",[]);
 viewName;
 modelName=smalltalk.send(tokens,"_second",[]);
 modelName;
-model=smalltalk.send(smalltalk.send((smalltalk.Trapped || Trapped),"_current",[]),"_byName_",[modelName]);
-model;
+return smalltalk.send((smalltalk.Trapped || Trapped),"_with_do_",[[modelName],(function(){
 return smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send((smalltalk.Smalltalk || Smalltalk),"_current",[]),"_at_",[viewName]),"_new",[]),"_appendToJQuery_",[jq]);
 })]);
+})]);
 return self},
 args: [],
-source: "start\x0a\x09'[data-trap]' asJQuery each: [ :index :elem |\x0a    \x09| trap jq viewName modelName tokens model view |\x0a        jq := elem asJQuery.\x0a        trap := jq attr: 'data-trap'.\x0a        tokens := trap tokenize: ':'.\x0a        viewName := tokens first.\x0a        modelName := tokens second.\x0a       \x09model := Trapped current byName: modelName.\x0a        \x22TODO do something with model\x22\x0a        (Smalltalk current at: viewName) new appendToJQuery: jq.\x0a    ]",
-messageSends: ["each:", "asJQuery", "attr:", "tokenize:", "first", "second", "byName:", "current", "appendToJQuery:", "new", "at:"],
-referencedClasses: ["Trapped", "Smalltalk"]
+source: "start\x0a\x09'[data-trap]' asJQuery each: [ :index :elem |\x0a    \x09| trap jq viewName modelName tokens |\x0a        jq := elem asJQuery.\x0a        trap := jq attr: 'data-trap'.\x0a        tokens := trap tokenize: ':'.\x0a        viewName := tokens first.\x0a        modelName := tokens second.\x0a        Trapped with: { modelName } do: [(Smalltalk current at: viewName) new appendToJQuery: jq].\x0a    ]",
+messageSends: ["each:", "asJQuery", "attr:", "tokenize:", "first", "second", "with:do:", "appendToJQuery:", "new", "at:", "current"],
+referencedClasses: ["Smalltalk", "Trapped"]
 }),
 smalltalk.Trapped);
 
 
+smalltalk.Trapped.klass.iVarNames = ['path'];
+smalltalk.addMethod(
+"_initialize",
+smalltalk.method({
+selector: "initialize",
+category: 'initialization',
+fn: function (){
+var self=this;
+self["@path"]=smalltalk.send((smalltalk.TrappedPathStack || TrappedPathStack),"_new",[]);
+return self},
+args: [],
+source: "initialize\x0a\x09path := TrappedPathStack new",
+messageSends: ["new"],
+referencedClasses: ["TrappedPathStack"]
+}),
+smalltalk.Trapped.klass);
 
-smalltalk.addClass('TrappedFly', smalltalk.TrappedSingleton, [], 'Trapped-Frontend');
 smalltalk.addMethod(
-"_name",
+"_path",
 smalltalk.method({
-selector: "name",
-category: 'action',
+selector: "path",
+category: 'accessing',
 fn: function (){
 var self=this;
-var $1;
-$1=smalltalk.send(smalltalk.send(self,"_class",[]),"_name",[]);
-return $1;
+return self["@path"];
 },
 args: [],
-source: "name\x0a\x09^ self class name",
-messageSends: ["name", "class"],
+source: "path\x0a\x09^path",
+messageSends: [],
 referencedClasses: []
 }),
-smalltalk.TrappedFly);
+smalltalk.Trapped.klass);
 
 smalltalk.addMethod(
-"_start",
+"_with_do_",
 smalltalk.method({
-selector: "start",
+selector: "with:do:",
 category: 'action',
-fn: function (){
+fn: function (anArray,aBlock){
 var self=this;
-smalltalk.send(smalltalk.send((smalltalk.Trapped || Trapped),"_current",[]),"_register_name_",[self,smalltalk.send(self,"_name",[])]);
+smalltalk.send(self["@path"],"_with_do_",[anArray,aBlock]);
 return self},
-args: [],
-source: "start\x0a\x09Trapped current register: self name: self name",
-messageSends: ["register:name:", "name", "current"],
-referencedClasses: ["Trapped"]
+args: ["anArray", "aBlock"],
+source: "with: anArray do: aBlock\x0a\x09path with: anArray do: aBlock",
+messageSends: ["with:do:"],
+referencedClasses: []
 }),
-smalltalk.TrappedFly);
-
+smalltalk.Trapped.klass);
 
 
 smalltalk.addClass('TrappedView', smalltalk.Widget, [], 'Trapped-Frontend');
@@ -197,12 +335,12 @@ category: 'rendering',
 fn: function (html){
 var self=this;
 smalltalk.send(smalltalk.send(html,"_root",[]),"_empty",[]);
-smalltalk.send(html,"_with_",[smalltalk.send(smalltalk.send(smalltalk.send(self,"_class",[]),"_name",[]),"__comma",[": contents"])]);
+smalltalk.send(html,"_with_",[smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(self,"_class",[]),"_name",[]),"__comma",[": "]),"__comma",[smalltalk.send(smalltalk.send((smalltalk.Trapped || Trapped),"_path",[]),"_elements",[])])]);
 return self},
 args: ["html"],
-source: "renderOn: html\x0a\x09html root empty.\x0a\x09html with: self class name, ': contents'",
-messageSends: ["empty", "root", "with:", ",", "name", "class"],
-referencedClasses: []
+source: "renderOn: html\x0a\x09html root empty.\x0a\x09html with: self class name, ': ', Trapped path elements",
+messageSends: ["empty", "root", "with:", ",", "elements", "path", "name", "class"],
+referencedClasses: ["Trapped"]
 }),
 smalltalk.TrappedView);
 

+ 67 - 14
st/Trapped-Frontend.st

@@ -1,4 +1,53 @@
 Smalltalk current createPackage: 'Trapped-Frontend' properties: #{}!
+Object subclass: #TrappedFly
+	instanceVariableNames: ''
+	package: 'Trapped-Frontend'!
+
+!TrappedFly methodsFor: 'action'!
+
+name
+	^ self class name
+!
+
+start
+	Trapped current register: self name: self name
+! !
+
+!TrappedFly class methodsFor: 'action'!
+
+start
+	self new start
+! !
+
+Object subclass: #TrappedPathStack
+	instanceVariableNames: 'elements'
+	package: 'Trapped-Frontend'!
+
+!TrappedPathStack methodsFor: 'accessing'!
+
+elements
+	^elements
+! !
+
+!TrappedPathStack methodsFor: 'descending'!
+
+append: anArray
+    elements := elements, anArray
+!
+
+with: anArray do: aBlock
+	| old |
+    old := elements.
+    [ self append: anArray.
+	aBlock value ] ensure: [ elements := old ]
+! !
+
+!TrappedPathStack methodsFor: 'initializing'!
+
+initialize
+	elements := #().
+! !
+
 Object subclass: #TrappedSingleton
 	instanceVariableNames: ''
 	package: 'Trapped-Frontend'!
@@ -41,15 +90,13 @@ register: aFly name: aString
 
 start
 	'[data-trap]' asJQuery each: [ :index :elem |
-    	| trap jq viewName modelName tokens model view |
+    	| trap jq viewName modelName tokens |
         jq := elem asJQuery.
         trap := jq attr: 'data-trap'.
         tokens := trap tokenize: ':'.
         viewName := tokens first.
         modelName := tokens second.
-       	model := Trapped current byName: modelName.
-        "TODO do something with model"
-        (Smalltalk current at: viewName) new appendToJQuery: jq.
+        Trapped with: { modelName } do: [(Smalltalk current at: viewName) new appendToJQuery: jq].
     ]
 ! !
 
@@ -60,18 +107,24 @@ initialize
 	registry := #{}.
 ! !
 
-TrappedSingleton subclass: #TrappedFly
-	instanceVariableNames: ''
-	package: 'Trapped-Frontend'!
+Trapped class instanceVariableNames: 'path'!
 
-!TrappedFly methodsFor: 'action'!
+!Trapped class methodsFor: 'accessing'!
 
-name
-	^ self class name
-!
+path
+	^path
+! !
 
-start
-	Trapped current register: self name: self name
+!Trapped class methodsFor: 'action'!
+
+with: anArray do: aBlock
+	path with: anArray do: aBlock
+! !
+
+!Trapped class methodsFor: 'initialization'!
+
+initialize
+	path := TrappedPathStack new
 ! !
 
 Widget subclass: #TrappedView
@@ -82,6 +135,6 @@ Widget subclass: #TrappedView
 
 renderOn: html
 	html root empty.
-	html with: self class name, ': contents'
+	html with: self class name, ': ', Trapped path elements
 ! !