Documentation.st 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. Smalltalk current createPackage: 'Documentation' properties: #{}!
  2. Object subclass: #DocumentationBuilder
  3. instanceVariableNames: 'chapters announcer widget'
  4. category: 'Documentation'!
  5. !DocumentationBuilder methodsFor: 'accessing'!
  6. chapters
  7. ^chapters ifNil: [chapters := self buildChapters]
  8. !
  9. announcer
  10. ^announcer ifNil: [announcer := Announcer new]
  11. !
  12. widget
  13. ^widget ifNil: [widget := DocumentationWidget on: self]
  14. ! !
  15. !DocumentationBuilder methodsFor: 'building'!
  16. buildChapters
  17. ^((self class methodDictionary values sorted: [:a :b | a selector < b selector])
  18. select: [:each | each category = 'chapters'])
  19. collect: [:each | self perform: each selector]
  20. !
  21. buildOn: aCanvas
  22. aCanvas with: self widget.
  23. self
  24. checkHashChange;
  25. checkHash
  26. !
  27. buildOnJQuery: aJQuery
  28. self buildOn: (HTMLCanvas onJQuery: aJQuery)
  29. !
  30. build
  31. self buildOnJQuery: ('body' asJQuery)
  32. ! !
  33. !DocumentationBuilder methodsFor: 'chapters'!
  34. ch1introduction
  35. ^DocChapter new
  36. title: 'Introduction';
  37. contents: '
  38. ##Amber Smalltalk in a nutshell
  39. Amber is an implementation of the Smalltalk-80 language. It is designed to make client-side web development **faster, easier and more fun** as it allows developers to write HTML5 applications in a live Smalltalk environment!!
  40. Amber is written in itself, including the IDE and the compiler and it runs **directly inside your browser**. The IDE is fairly complete with a class browser, workspace, transcript, unit test runner, object inspectors, cross reference tools and even a debugger.
  41. Noteworthy features:
  42. - Amber is semantically and syntactically very close to [Pharo Smalltalk](http://www.pharo-project.org). Pharo is considered the reference implementation.
  43. - Amber **seamlessly interacts with JavaScript** and can use its full eco system of libraries without any glue code needed.
  44. - Amber **has no dependencies** and can be used in any JavaScript runtime, not only inside browsers. An important example is [Node.js](http://nodejs.org).
  45. - Amber is a live Smalltalk that **compiles incrementally into efficient JavaScript** often mapping one-to-one with JavaScript equivalents.
  46. - Amber has a **Seaside influenced canvas library** to dynamically generate HTML.
  47. ## Disclaimer
  48. This documentation doesn''t aim to teach Smalltalk.
  49. Knowledge of Smalltalk is needed to understand the topics covered in this documentation.
  50. If you want to learn the Smalltalk language, you can read the excellent [Pharo By Example](http://www.pharobyexample.org) book.
  51. '
  52. !
  53. ch2differencesWithOtherSmalltalks
  54. ^DocChapter new
  55. title: 'Differences with other Smalltalks';
  56. contents: '
  57. Amber has some differences with other Smalltalk implementations.
  58. Because it maps Smalltalk constructs one-to-one with the JavaScript equivalent, including Smalltalk classes to JavaScript constructors, the core class library is simplified compared to Pharo Smalltalk.
  59. The following list explains the main differences:
  60. - The collection class hierarchy is simpler compared to most Smalltalk implementations. As of today, there is no SortedCollection. The size of arrays is dynamic, and they behave like an ordered collection.
  61. They can also be sorted with the #sort* methods.
  62. - The Date class behaves like the Date and TimeStamp classes in Pharo Smalltalk. Therefore both Date today and Date now are valid in Amber.
  63. '
  64. !
  65. ch3GettingStarted
  66. ^DocChapter new
  67. title: 'Getting started';
  68. contents: '
  69. To get started hacking in Amber you can basically take three routes, independent of your platform:
  70. 1. Just try it out at http://www.amber-lang.net (click the "Class browser" button) - but you will **not be able to save any code you write**!!
  71. Still, it works fine for looking at the IDE and playing around. Just don''t press F5/reload - it will bring you back to zero.
  72. (Well, if you still want to develop and save code online someone has set up this site seems for free use: https://www.screwtopdb.com/amberstore/topics?name=amberstore/amber.html )
  73. 2. Download an Amber zip-ball, install Nodejs, fire up the Amber server and then open Amber from localhost - then you **can save code**. Detailed instructions are below!!
  74. 3. Same as above but install git first and get a proper clone from http://github.com/NicolasPetton/amber instead of a zip/tar-ball.
  75. If you want to **contribute to Amber itself** this is really what you want to do. It requires installing git first, but it is quite simple - although we leave this bit as an "exercise to the reader" :)
  76. **PLEASE NOTE:** Amber core developers use Linux.
  77. We do not want to introduce dependencies that aren''t cross platform - but currently amberc (the command line compiler) is a bash script and we also use Makefiles
  78. (for building Amber itself and server side examples) written on Linux/Unix. So using Windows is currently a bit limited - you can''t run "make" in the .st directory to rebuild whole of Amber for example.
  79. BUT... if you only want to use Amber to build web client apps and not really get involved in hacking Amber itself - then you should be fine!!
  80. ## Downloading Amber
  81. Currently you can download in zip or tar-ball format, either cutting edge or a release. [Downloads are available here](https://github.com/NicolasPetton/amber/archives/amber).
  82. At the moment of writing you have release [0.9 as zip](https://github.com/NicolasPetton/amber/zipball/0.9) or [tar](https://github.com/NicolasPetton/amber/tarball/0.9),
  83. or [cutting edge as zip](https://github.com/NicolasPetton/amber/zipball/amber) or [tar](https://github.com/NicolasPetton/amber/tarball/amber).
  84. At the moment this is just a **1.5Mb download**, so its very small. Unpack wherever you like, but I would rename the directory that is unpacked to something slightly shorter - like say "amber-0.9" or just "amber".
  85. And yes, at this point you can double click the index.html file in the amber directory to get the IDE up, but again, **you will not be able to save code**. So please continue below :)
  86. ## Installing Node.js
  87. [Node](http://www.nodejs.org) (for short) is simply the V8 Javascript VM from Google (used in Chrome) hooked together with some hard core C-libraries for doing "evented I/O".
  88. Basically it''s Javascript for the server - on asynch steroids. Amber runs fine in Node and we use it for several Amber tools, like amberc (the command line Amber compiler) or the Amber server (see below).
  89. There are also several Amber-Node example to look at if you want to play with running Amber programs server side. **In short - you really want to install Nodejs. :)**
  90. - Installing Node on Linux can be done using your package tool of choice ("apt-get install nodejs" for example) or any other way described at [the download page](http://nodejs.org/#download).
  91. - Installing Node on MacOS seems to be easiest by getting it from [here](https://sites.google.com/site/nodejsmacosx/).
  92. - Installing Node on Windows is probably done best by using the [download from Nodejs.org](http://nodejs.org/#download). This is not an installer, it is instead simply the node executable - **node.exe**.
  93. - Put node.exe somewhere in your path. In Windows7 I can run a command prompt "As administrator" (right click the command prompt in Start menu) and then just "copy node.exe c:\windows\" or such.
  94. ## Starting Amber server
  95. Nicolas has written a minimal webDAV server that is the easiest way to get up and running Amber with the ability to save code. This little server is written in... Amber!!
  96. And it runs on top of Node. So to start it up serving your brand new directory tree of sweet Amber you do:
  97. cd amber (or whatever you called the directory you unpackaged)
  98. ./bin/server (in windows you type "node server\server.js" instead)
  99. It should say it is listening on port 4000. If it does, hooray!! That means both Node and Amber are good. In Windows you might get a question about opening that port in the local firewall - yep, do it!!
  100. ## Firing up Amber
  101. The Amber IDE is written in... Amber. It uses JQuery and runs right in your browser as a ... well, a web page.
  102. We could open it up just using a file url - but the reason we performed the previous steps is so that we can load the IDE web page from a server that can handle PUTs (webDAV) of source code.
  103. According to web security Amber can only do PUT back to the same server it was loaded from. Thus we instead want to open it through our little server now listening on port 4000:
  104. http://localhost:4000/index.html
  105. Clicking the above link should get your Amber running.
  106. To verify that you can indeed commit - just select a Package in the browser, like say "Examples" and press "Commit package" button. **If all goes well nothing happens :)**.
  107. So in order to really know if it worked we can check the modified date on the files **amber/st/Examples.st**, **amber/js/Examples.js** and **amber/js/Examples.deploy.js** - they should be brand new.
  108. NOTE: We can use any webDAV server and Apache2 has been used earlier and works fine. But the Amber server is smaller and simpler to start.
  109. '
  110. !
  111. ch5Index
  112. ^ClassesIndexChapter new
  113. !
  114. ch6KernelObjects
  115. ^PackageDocChapter on: (Package named: 'Kernel-Objects')
  116. !
  117. ch7KernelClasses
  118. ^PackageDocChapter on: (Package named: 'Kernel-Classes')
  119. !
  120. ch4Tutorials
  121. ^TutorialsChapter new
  122. ! !
  123. !DocumentationBuilder methodsFor: 'routing'!
  124. checkHashChange
  125. (window jQuery: window) bind: 'hashchange' do: [self checkHash]
  126. !
  127. checkHash
  128. | hash presentation |
  129. hash := document location hash replace: '^#' with: ''.
  130. self announcer announce: (ChapterSelectionAnnouncement new
  131. id: hash;
  132. yourself)
  133. ! !
  134. !DocumentationBuilder methodsFor: 'updating'!
  135. update
  136. chapters := nil.
  137. announcer := nil.
  138. widget := nil.
  139. (window jQuery: '.documentation') remove.
  140. self build
  141. ! !
  142. DocumentationBuilder class instanceVariableNames: 'current'!
  143. !DocumentationBuilder class methodsFor: 'accessing'!
  144. current
  145. ^current ifNil: [current := self new]
  146. ! !
  147. !DocumentationBuilder class methodsFor: 'initialization'!
  148. initialize
  149. self current build
  150. ! !
  151. Widget subclass: #DocChapter
  152. instanceVariableNames: 'title contents parent'
  153. category: 'Documentation'!
  154. !DocChapter methodsFor: 'accessing'!
  155. title
  156. ^title ifNil: ['']
  157. !
  158. title: aString
  159. title := aString
  160. !
  161. contents
  162. ^contents ifNil: ['']
  163. !
  164. contents: aString
  165. contents := aString
  166. !
  167. htmlContents
  168. ^(Showdown at: #converter) new makeHtml: self contents
  169. !
  170. chapters
  171. "A doc chapter can contain sub chapters"
  172. ^#()
  173. !
  174. cssClass
  175. ^'doc_chapter'
  176. !
  177. level
  178. ^self parent ifNil: [1] ifNotNil: [self parent level +1]
  179. !
  180. level: anInteger
  181. level := anInteger
  182. !
  183. parent
  184. ^parent
  185. !
  186. parent: aChapter
  187. parent := aChapter
  188. !
  189. id
  190. "The id is used in url fragments.
  191. It must be unique amoung all chapters"
  192. ^self title replace: ' ' with: '-'
  193. !
  194. announcer
  195. ^DocumentationBuilder current announcer
  196. ! !
  197. !DocChapter methodsFor: 'actions'!
  198. selectClass: aClass
  199. DocumentationBuilder current announcer announce: (ClassSelectionAnnouncement on: aClass)
  200. !
  201. selectChapter: aChapter
  202. document location hash: aChapter id
  203. !
  204. displayChapter: aChapter
  205. DocumentationBuilder current widget displayChapter: aChapter
  206. ! !
  207. !DocChapter methodsFor: 'initialization'!
  208. initialize
  209. super initialize.
  210. self subscribe
  211. ! !
  212. !DocChapter methodsFor: 'rendering'!
  213. renderOn: html
  214. html div
  215. class: self cssClass;
  216. with: [
  217. self renderDocOn: html.
  218. self renderLinksOn: html]
  219. !
  220. renderDocOn: html
  221. | div |
  222. html h1 with: self title.
  223. self renderNavigationOn: html.
  224. div := html div class: 'contents'.
  225. div asJQuery html: self htmlContents
  226. !
  227. renderNavigationOn: html
  228. self parent ifNotNil: [
  229. html div
  230. class: 'navigation'; with: [
  231. html a
  232. with: '← back to ', self parent title;
  233. onClick: [self selectChapter: self parent]]]
  234. !
  235. renderLinksOn: html
  236. html ul
  237. class: 'links';
  238. with: [
  239. self chapters do: [:each |
  240. html li with: [
  241. html a
  242. with: each title;
  243. onClick: [self selectChapter: each]]]]
  244. ! !
  245. !DocChapter methodsFor: 'subscriptions'!
  246. subscribe
  247. self announcer on: ChapterSelectionAnnouncement do: [:ann |
  248. ann id = self id ifTrue: [self displayChapter: self]]
  249. ! !
  250. DocChapter subclass: #PackageDocChapter
  251. instanceVariableNames: 'package chapters'
  252. category: 'Documentation'!
  253. !PackageDocChapter methodsFor: 'accessing'!
  254. package
  255. ^package
  256. !
  257. title
  258. ^'Package ', self package name
  259. !
  260. chapters
  261. ^chapters
  262. !
  263. contents
  264. ^'Classes in package ', self package name, ':'
  265. ! !
  266. !PackageDocChapter methodsFor: 'initialization'!
  267. initializeWithPackage: aPackage
  268. package := aPackage.
  269. chapters := (aPackage classes sorted: [:a :b | a name < b name]) collect: [:each |
  270. (ClassDocChapter on: each)
  271. parent: self;
  272. yourself]
  273. ! !
  274. !PackageDocChapter class methodsFor: 'instance creation'!
  275. on: aPackage
  276. ^self basicNew
  277. initializeWithPackage: aPackage;
  278. initialize;
  279. yourself
  280. ! !
  281. DocChapter subclass: #ClassDocChapter
  282. instanceVariableNames: 'theClass'
  283. category: 'Documentation'!
  284. !ClassDocChapter methodsFor: 'accessing'!
  285. theClass
  286. ^theClass
  287. !
  288. contents
  289. ^self theClass comment isEmpty
  290. ifTrue: [self theClass name, ' is not documented yet.']
  291. ifFalse: [self theClass comment]
  292. !
  293. cssClass
  294. ^'doc_class ', super cssClass
  295. !
  296. title
  297. ^self theClass name
  298. !
  299. initializeWithClass: aClass
  300. theClass := aClass
  301. ! !
  302. !ClassDocChapter methodsFor: 'rendering'!
  303. renderLinksOn: html
  304. html ul
  305. class: 'links';
  306. with: [
  307. html li with: [html a
  308. with: 'Browse this class';
  309. onClick: [Browser openOn: self theClass]]]
  310. ! !
  311. !ClassDocChapter methodsFor: 'subscriptions'!
  312. subscribe
  313. super subscribe.
  314. self announcer
  315. on: ClassSelectionAnnouncement do: [:ann |
  316. ann theClass = self theClass ifTrue: [
  317. self selectChapter: self]]
  318. ! !
  319. !ClassDocChapter class methodsFor: 'accessing'!
  320. on: aClass
  321. ^self basicNew
  322. initializeWithClass: aClass;
  323. initialize;
  324. yourself
  325. ! !
  326. Widget subclass: #DocumentationWidget
  327. instanceVariableNames: 'builder selectedChapter chapterDiv'
  328. category: 'Documentation'!
  329. !DocumentationWidget methodsFor: 'accessing'!
  330. builder
  331. ^builder
  332. !
  333. builder: aDocumentationBuilder
  334. builder := aDocumentationBuilder
  335. !
  336. chapters
  337. ^self builder chapters
  338. !
  339. selectedChapter
  340. ^selectedChapter ifNil: [selectedChapter := self chapters first]
  341. !
  342. selectedChapter: aChapter
  343. ^selectedChapter := aChapter
  344. ! !
  345. !DocumentationWidget methodsFor: 'actions'!
  346. displayChapter: aChapter
  347. self selectedChapter: aChapter.
  348. self updateChapterDiv
  349. !
  350. selectChapter: aChapter
  351. document location hash: aChapter id
  352. ! !
  353. !DocumentationWidget methodsFor: 'rendering'!
  354. renderOn: html
  355. html div
  356. class: 'documentation';
  357. with: [
  358. self renderMenuOn: html.
  359. chapterDiv := html div.
  360. self updateChapterDiv]
  361. !
  362. renderMenuOn: html
  363. html div
  364. class: 'menu';
  365. with: [
  366. html ol with: [
  367. self chapters do: [:each |
  368. html li with: [
  369. self renderChapterMenu: each on: html]]]]
  370. !
  371. renderChapterMenu: aChapter on: html
  372. html a
  373. with: aChapter title;
  374. onClick: [
  375. self selectChapter: aChapter].
  376. html ol with: [
  377. aChapter chapters do: [:each |
  378. html li with: [
  379. self renderChapterMenu: each on: html]]]
  380. ! !
  381. !DocumentationWidget methodsFor: 'updating'!
  382. updateChapterDiv
  383. chapterDiv contents: [:html |
  384. html with: self selectedChapter]
  385. ! !
  386. !DocumentationWidget class methodsFor: 'instance creation'!
  387. on: aBuilder
  388. ^self new
  389. builder: aBuilder;
  390. yourself
  391. ! !
  392. DocChapter subclass: #ClassesIndexChapter
  393. instanceVariableNames: ''
  394. category: 'Documentation'!
  395. !ClassesIndexChapter methodsFor: 'accessing'!
  396. cssClass
  397. ^'index_doc ', super cssClass
  398. !
  399. title
  400. ^'Smalltalk classes by index'
  401. !
  402. alphabet
  403. ^'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  404. ! !
  405. !ClassesIndexChapter methodsFor: 'rendering'!
  406. renderDocOn: html
  407. html h1 with: self title.
  408. self alphabet do: [:letter || classes |
  409. classes := Smalltalk current classes select: [:each | each name first = letter].
  410. classes ifNotEmpty: [html h2 with: letter].
  411. html ul with: [
  412. (classes sorted: [:a :b | a name < b name])
  413. do: [:each |
  414. html li with: [html a
  415. with: each name;
  416. onClick: [self selectClass: each]]]]]
  417. ! !
  418. Object subclass: #ClassSelectionAnnouncement
  419. instanceVariableNames: 'theClass'
  420. category: 'Documentation'!
  421. !ClassSelectionAnnouncement methodsFor: 'accessing'!
  422. theClass
  423. ^theClass
  424. !
  425. theClass: aClass
  426. theClass := aClass
  427. ! !
  428. !ClassSelectionAnnouncement class methodsFor: 'instance creation'!
  429. on: aClass
  430. ^self new
  431. theClass: aClass;
  432. yourself
  433. ! !
  434. Object subclass: #ChapterSelectionAnnouncement
  435. instanceVariableNames: 'id'
  436. category: 'Documentation'!
  437. !ChapterSelectionAnnouncement methodsFor: 'accessing'!
  438. id
  439. ^id
  440. !
  441. id: aString
  442. id := aString
  443. ! !
  444. DocChapter subclass: #TutorialsChapter
  445. instanceVariableNames: ''
  446. category: 'Documentation'!
  447. !TutorialsChapter methodsFor: 'accessing'!
  448. title
  449. ^'Tutorials'
  450. !
  451. contents
  452. ^'Here''s a serie of tutorials. If you are new to Smalltalk, you can also learn Amber online with [ProfStef](http://www.amber-lang.net/learn.html)'
  453. !
  454. chapters
  455. ^{ self firstAppChapter. self counterChapter }
  456. !
  457. firstAppChapter
  458. ^DocChapter new
  459. title: 'A first application';
  460. contents: '
  461. Let''s make Hello World in Amber.
  462. First, you need a place for your new project. I made a new directory under amber:
  463. amber/projects/hello
  464. This will store your project files. To get started, add a new index.html file to this folder, as well as empty js and st folders.
  465. Your index.html can be really basic. The most important thing it does is include amber.js and run loadAmber. Here is a basic index.html you can use:
  466. <!!DOCTYPE html>
  467. <html>
  468. <head>
  469. <title>My First Amber Project</title>
  470. <script src="../../js/amber.js" type="text/javascript"></script>
  471. <script type="text/javascript">
  472. loadAmber({
  473. files: [],
  474. prefix: ''projects/hello/js'',
  475. ready: function() {
  476. }});
  477. </script>
  478. </head>
  479. <body>
  480. <article>
  481. <h1>My First Amber Project</h1>
  482. <button onclick="smalltalk.Browser._open()">class browser</button>
  483. <button id="sayHello">say hello</button>
  484. </article>
  485. </body>
  486. </html>
  487. Now start up amber with node.js and navigate to http://localhost:4000/projects/hello/index.html
  488. It''s boring so far, so lets write some code. Click the button to open the class browser. Find an existing class and change its name to Hello and its package to HelloApp.
  489. Then click save. This creates a new class and leaves the old one intact, it doesn''t overwrite it. Your class will look like this:
  490. Object subclass: #Hello
  491. instanceVariableNames: ''''
  492. package: ''HelloApp''
  493. Now click save and navigate to your new class in its new package.
  494. Then click ''commit package''. You just created a new class and saved your work.
  495. On your file system check out your js and st folders. Your new class is now saved in both JavaScript and Smalltalk.
  496. Now, refresh your browser page and reopen the class browser. Oh no, your new class is gone!! To load your new class automatically, you have to add it in index.html. Make your JavaScript look like this:
  497. loadAmber({
  498. files: [''HelloApp.js''],
  499. prefix: ''projects/hello/js'',
  500. ready: function() {
  501. }});
  502. Save and refresh again. Now your class is loaded and shows up in the class browser.
  503. Now, let''s make this class do something. Create a new message in the class browser by navigating to your class, then clicking ''not yet classified'' and fill in a simple message. Try this for example:
  504. begin
  505. "Makes me say hello to the user."
  506. | msg button |
  507. msg := ''Hello world!!''.
  508. button := ''#sayHello'' asJQuery.
  509. button click: [button after: ''<p>'' , msg , ''</p>''].
  510. Your message isn''t too helpful if it doesn''t get called. Save it, commit the package, then edit index.html again. You can write JavaScript code that sends a message to Smalltalk:
  511. loadAmber({
  512. files: [''HelloApp.js''],
  513. prefix: ''projects/hello/js'', // path for js files i think
  514. ready: function() {
  515. $(function() {
  516. smalltalk.Hello._new()._begin();
  517. });
  518. }});
  519. From there, you can create new Smalltalk classes and messages to build up your app. Enjoy!!
  520. '
  521. !
  522. counterChapter
  523. ^DocChapter new
  524. title: 'The counter application';
  525. contents: '
  526. This tutorial will teach you how to build HTML with Amber using jQuery and the HTMLCanvas API. It is freely adapted from
  527. the [Seaside counter example](http://www.seaside.st/about/examples/counter)
  528. ##The counter widget
  529. The counter is the most basic example of a widget. It allows to increment and decrement a number by clicking a button.
  530. Amber already comes with a counter example in the `Examples` package. To avoid class name conflict, we''ll name our counter class `TCounter`.
  531. Widget subclass: #TCounter
  532. instanceVariableNames: ''count header''
  533. package: ''Tutorials''
  534. The first method is used to initialize the component with the default state, in this case we set the counter to 0:
  535. initialize
  536. super initialize.
  537. count := 0
  538. The method used for rendering a widget is `#renderOn:`. It takes an instance of HTMLCanvas as parameter.
  539. The `header` h1 kept as an instance variable, so when the count value change, we can update it''s contents accordingly.
  540. renderOn: html
  541. header := html h1
  542. with: count asString;
  543. yourself.
  544. html button
  545. with: ''++'';
  546. onClick: [self increase].
  547. html button
  548. with: ''--'';
  549. onClick: [self decrease]
  550. The counter is almost ready. All we need now is to implement the two action methods `#increase` and `#decrease` to change the state
  551. of our counter and update its header.
  552. increase
  553. count := count + 1.
  554. header contents: [:html | html with: count asString]
  555. decrease
  556. count := count - 1.
  557. header contents: [:html | html with: count asString]
  558. That''s it!! We can now display an instance of TCounter by rendering it on the page using jQuery:
  559. TCounter new appendToJQuery: ''body'' asJQuery
  560. '
  561. ! !