Documentation.st 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. ## Arguments for using Amber
  48. In our humble opinion the main arguments for using Amber are:
  49. - JavaScript is quite a broken language with lots of traps and odd quirks. It is the assembler of the Internet which is cool, but we don''t want to write in it.
  50. - Smalltalk as a language is immensely cleaner and more mature, both syntactically and semantically.
  51. - Smalltalk has a simple class model with a lightweight syntax for closures, it is in many ways a perfect match for the Good Parts of JavaScript.
  52. - Having a true live interactive incremental development environment where you can build your application directly in the browser is unbeatable.
  53. ## Disclaimer
  54. This documentation doesn''t aim to teach Smalltalk.
  55. Knowledge of Smalltalk is needed to understand the topics covered in this documentation.
  56. If you want to learn the Smalltalk language, you can read the excellent [Pharo By Example](http://www.pharobyexample.org) book.
  57. '
  58. !
  59. ch2differencesWithOtherSmalltalks
  60. ^DocChapter new
  61. title: 'Differences with other Smalltalks';
  62. contents: '
  63. Amber has some differences with other Smalltalk implementations. This makes porting code a non-trivial thing, but still quite manageable.
  64. 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.
  65. And since we want Amber to be useful in building lean browser apps we can''t let it bloat too much.
  66. But apart from missing things other Smalltalks may have, there are also things that are plain different:
  67. - The collection class hierarchy is much simpler compared to most Smalltalk implementations.
  68. - As of today, there is no SortedCollection. The size of arrays is dynamic, and they behave like an ordered collection. They can also be sorted with the `#sort*` methods.
  69. - The `Date` class behaves like the `Date` and `TimeStamp` classes in Pharo Smalltalk. Therefore both `Date today` and `Date now` are valid in Amber.
  70. - Amber does not have class Character, but `String` does implement some of Character behavior.
  71. - Amber does support class instance variables, but not class variables.
  72. - Amber only has global classes and packages, but not arbitrary objects. Use classes instead like `Smalltalk current` instead of `Smalltalk` etc.
  73. - Amber does not support pool dictionaries.
  74. - Amber uses **< ...javascript code... >** to inline JavaScript code and does not have pragmas.
  75. - Amber does not have class categories. The left side in the browser lists real Packages, but they feel much the same.
  76. '
  77. !
  78. ch3GettingStarted
  79. ^DocChapter new
  80. title: 'Getting started';
  81. contents: '
  82. To get started hacking in Amber you can basically take three routes, independent of your platform:
  83. 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**!!
  84. 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.
  85. (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 )
  86. 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!!
  87. 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.
  88. 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" :)
  89. **PLEASE NOTE:** Amber core developers use Linux.
  90. 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
  91. (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.
  92. 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!!
  93. ## Downloading Amber
  94. 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).
  95. 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),
  96. or [cutting edge as zip](https://github.com/NicolasPetton/amber/zipball/amber) or [tar](https://github.com/NicolasPetton/amber/tarball/amber).
  97. 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".
  98. 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 :)
  99. ## Installing Node.js
  100. [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".
  101. 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).
  102. 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. :)**
  103. - 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).
  104. - Installing Node on MacOS seems to be easiest by getting it from [here](https://sites.google.com/site/nodejsmacosx/).
  105. - 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**.
  106. - 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.
  107. ## Starting Amber server
  108. 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!!
  109. And it runs on top of Node. So to start it up serving your brand new directory tree of sweet Amber you do:
  110. cd amber (or whatever you called the directory you unpackaged)
  111. ./bin/server (in windows you type "node server\server.js" instead)
  112. 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!!
  113. ## Firing up Amber
  114. The Amber IDE is written in... Amber. It uses JQuery and runs right in your browser as a ... well, a web page.
  115. 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.
  116. 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:
  117. http://localhost:4000/index.html
  118. Clicking the above link should get your Amber running.
  119. 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 :)**.
  120. 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.
  121. 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.
  122. '
  123. !
  124. ch5Index
  125. ^ClassesIndexChapter new
  126. !
  127. ch6KernelObjects
  128. ^PackageDocChapter on: (Package named: 'Kernel-Objects')
  129. !
  130. ch7KernelClasses
  131. ^PackageDocChapter on: (Package named: 'Kernel-Classes')
  132. !
  133. ch4Tutorials
  134. ^TutorialsChapter new
  135. !
  136. ch8KernelCollection
  137. ^PackageDocChapter on: (Package named: 'Kernel-Collections')
  138. !
  139. ch9KernelMethods
  140. ^PackageDocChapter on: (Package named: 'Kernel-Methods')
  141. ! !
  142. !DocumentationBuilder methodsFor: 'routing'!
  143. checkHashChange
  144. (window jQuery: window) bind: 'hashchange' do: [self checkHash]
  145. !
  146. checkHash
  147. | hash presentation |
  148. hash := document location hash replace: '^#' with: ''.
  149. self announcer announce: (ChapterSelectionAnnouncement new
  150. id: hash;
  151. yourself)
  152. ! !
  153. !DocumentationBuilder methodsFor: 'updating'!
  154. update
  155. chapters := nil.
  156. announcer := nil.
  157. widget := nil.
  158. (window jQuery: '.documentation') remove.
  159. self build
  160. ! !
  161. DocumentationBuilder class instanceVariableNames: 'current'!
  162. !DocumentationBuilder class methodsFor: 'accessing'!
  163. current
  164. ^current ifNil: [current := self new]
  165. ! !
  166. !DocumentationBuilder class methodsFor: 'initialization'!
  167. initialize
  168. self current build
  169. ! !
  170. Widget subclass: #DocChapter
  171. instanceVariableNames: 'title contents parent'
  172. category: 'Documentation'!
  173. !DocChapter methodsFor: 'accessing'!
  174. title
  175. ^title ifNil: ['']
  176. !
  177. title: aString
  178. title := aString
  179. !
  180. contents
  181. ^contents ifNil: ['']
  182. !
  183. contents: aString
  184. contents := aString
  185. !
  186. htmlContents
  187. ^(Showdown at: #converter) new makeHtml: self contents
  188. !
  189. chapters
  190. "A doc chapter can contain sub chapters"
  191. ^#()
  192. !
  193. cssClass
  194. ^'doc_chapter'
  195. !
  196. level
  197. ^self parent ifNil: [1] ifNotNil: [self parent level +1]
  198. !
  199. level: anInteger
  200. level := anInteger
  201. !
  202. parent
  203. ^parent
  204. !
  205. parent: aChapter
  206. parent := aChapter
  207. !
  208. id
  209. "The id is used in url fragments.
  210. It must be unique amoung all chapters"
  211. ^self title replace: ' ' with: '-'
  212. !
  213. announcer
  214. ^DocumentationBuilder current announcer
  215. ! !
  216. !DocChapter methodsFor: 'actions'!
  217. selectClass: aClass
  218. DocumentationBuilder current announcer announce: (ClassSelectionAnnouncement on: aClass)
  219. !
  220. selectChapter: aChapter
  221. document location hash: aChapter id
  222. !
  223. displayChapter: aChapter
  224. DocumentationBuilder current widget displayChapter: aChapter
  225. ! !
  226. !DocChapter methodsFor: 'initialization'!
  227. initialize
  228. super initialize.
  229. self subscribe
  230. ! !
  231. !DocChapter methodsFor: 'rendering'!
  232. renderOn: html
  233. html div
  234. class: self cssClass;
  235. with: [
  236. self renderDocOn: html.
  237. self renderLinksOn: html]
  238. !
  239. renderDocOn: html
  240. | div |
  241. html h1 with: self title.
  242. self renderNavigationOn: html.
  243. div := html div class: 'contents'.
  244. div asJQuery html: self htmlContents
  245. !
  246. renderNavigationOn: html
  247. self parent ifNotNil: [
  248. html div
  249. class: 'navigation'; with: [
  250. html a
  251. with: '← back to ', self parent title;
  252. onClick: [self selectChapter: self parent]]]
  253. !
  254. renderLinksOn: html
  255. html ul
  256. class: 'links';
  257. with: [
  258. self chapters do: [:each |
  259. html li with: [
  260. html a
  261. with: each title;
  262. onClick: [self selectChapter: each]]]]
  263. ! !
  264. !DocChapter methodsFor: 'subscriptions'!
  265. subscribe
  266. self announcer on: ChapterSelectionAnnouncement do: [:ann |
  267. ann id = self id ifTrue: [self displayChapter: self]]
  268. ! !
  269. DocChapter subclass: #PackageDocChapter
  270. instanceVariableNames: 'package chapters'
  271. category: 'Documentation'!
  272. !PackageDocChapter methodsFor: 'accessing'!
  273. package
  274. ^package
  275. !
  276. title
  277. ^'Package ', self package name
  278. !
  279. chapters
  280. ^chapters
  281. !
  282. contents
  283. ^'Classes in package ', self package name, ':'
  284. ! !
  285. !PackageDocChapter methodsFor: 'initialization'!
  286. initializeWithPackage: aPackage
  287. package := aPackage.
  288. chapters := (aPackage classes sorted: [:a :b | a name < b name]) collect: [:each |
  289. (ClassDocChapter on: each)
  290. parent: self;
  291. yourself]
  292. ! !
  293. !PackageDocChapter class methodsFor: 'instance creation'!
  294. on: aPackage
  295. ^self basicNew
  296. initializeWithPackage: aPackage;
  297. initialize;
  298. yourself
  299. ! !
  300. DocChapter subclass: #ClassDocChapter
  301. instanceVariableNames: 'theClass'
  302. category: 'Documentation'!
  303. !ClassDocChapter methodsFor: 'accessing'!
  304. theClass
  305. ^theClass
  306. !
  307. contents
  308. ^self theClass comment isEmpty
  309. ifTrue: [self theClass name, ' is not documented yet.']
  310. ifFalse: [self theClass comment]
  311. !
  312. cssClass
  313. ^'doc_class ', super cssClass
  314. !
  315. title
  316. ^self theClass name
  317. !
  318. initializeWithClass: aClass
  319. theClass := aClass
  320. ! !
  321. !ClassDocChapter methodsFor: 'rendering'!
  322. renderLinksOn: html
  323. html ul
  324. class: 'links';
  325. with: [
  326. html li with: [html a
  327. with: 'Browse this class';
  328. onClick: [Browser openOn: self theClass]]]
  329. ! !
  330. !ClassDocChapter methodsFor: 'subscriptions'!
  331. subscribe
  332. super subscribe.
  333. self announcer
  334. on: ClassSelectionAnnouncement do: [:ann |
  335. ann theClass = self theClass ifTrue: [
  336. self selectChapter: self]]
  337. ! !
  338. !ClassDocChapter class methodsFor: 'accessing'!
  339. on: aClass
  340. ^self basicNew
  341. initializeWithClass: aClass;
  342. initialize;
  343. yourself
  344. ! !
  345. Widget subclass: #DocumentationWidget
  346. instanceVariableNames: 'builder selectedChapter chapterDiv'
  347. category: 'Documentation'!
  348. !DocumentationWidget methodsFor: 'accessing'!
  349. builder
  350. ^builder
  351. !
  352. builder: aDocumentationBuilder
  353. builder := aDocumentationBuilder
  354. !
  355. chapters
  356. ^self builder chapters
  357. !
  358. selectedChapter
  359. ^selectedChapter ifNil: [selectedChapter := self chapters first]
  360. !
  361. selectedChapter: aChapter
  362. ^selectedChapter := aChapter
  363. ! !
  364. !DocumentationWidget methodsFor: 'actions'!
  365. displayChapter: aChapter
  366. self selectedChapter: aChapter.
  367. self updateChapterDiv
  368. !
  369. selectChapter: aChapter
  370. document location hash: aChapter id
  371. ! !
  372. !DocumentationWidget methodsFor: 'rendering'!
  373. renderOn: html
  374. html div
  375. class: 'documentation';
  376. with: [
  377. self renderMenuOn: html.
  378. chapterDiv := html div.
  379. self updateChapterDiv]
  380. !
  381. renderMenuOn: html
  382. html div
  383. class: 'menu';
  384. with: [
  385. html ol with: [
  386. self chapters do: [:each |
  387. html li with: [
  388. self renderChapterMenu: each on: html]]]]
  389. !
  390. renderChapterMenu: aChapter on: html
  391. html a
  392. with: aChapter title;
  393. onClick: [
  394. self selectChapter: aChapter].
  395. html ol with: [
  396. aChapter chapters do: [:each |
  397. html li with: [
  398. self renderChapterMenu: each on: html]]]
  399. ! !
  400. !DocumentationWidget methodsFor: 'updating'!
  401. updateChapterDiv
  402. chapterDiv contents: [:html |
  403. html with: self selectedChapter]
  404. ! !
  405. !DocumentationWidget class methodsFor: 'instance creation'!
  406. on: aBuilder
  407. ^self new
  408. builder: aBuilder;
  409. yourself
  410. ! !
  411. DocChapter subclass: #ClassesIndexChapter
  412. instanceVariableNames: ''
  413. category: 'Documentation'!
  414. !ClassesIndexChapter methodsFor: 'accessing'!
  415. cssClass
  416. ^'index_doc ', super cssClass
  417. !
  418. title
  419. ^'Smalltalk classes by index'
  420. !
  421. alphabet
  422. ^'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  423. ! !
  424. !ClassesIndexChapter methodsFor: 'rendering'!
  425. renderDocOn: html
  426. html h1 with: self title.
  427. self alphabet do: [:letter || classes |
  428. classes := Smalltalk current classes select: [:each | each name first = letter].
  429. classes ifNotEmpty: [html h2 with: letter].
  430. html ul with: [
  431. (classes sorted: [:a :b | a name < b name])
  432. do: [:each |
  433. html li with: [html a
  434. with: each name;
  435. onClick: [self selectClass: each]]]]]
  436. ! !
  437. Object subclass: #ClassSelectionAnnouncement
  438. instanceVariableNames: 'theClass'
  439. category: 'Documentation'!
  440. !ClassSelectionAnnouncement methodsFor: 'accessing'!
  441. theClass
  442. ^theClass
  443. !
  444. theClass: aClass
  445. theClass := aClass
  446. ! !
  447. !ClassSelectionAnnouncement class methodsFor: 'instance creation'!
  448. on: aClass
  449. ^self new
  450. theClass: aClass;
  451. yourself
  452. ! !
  453. Object subclass: #ChapterSelectionAnnouncement
  454. instanceVariableNames: 'id'
  455. category: 'Documentation'!
  456. !ChapterSelectionAnnouncement methodsFor: 'accessing'!
  457. id
  458. ^id
  459. !
  460. id: aString
  461. id := aString
  462. ! !
  463. DocChapter subclass: #TutorialsChapter
  464. instanceVariableNames: ''
  465. category: 'Documentation'!
  466. !TutorialsChapter methodsFor: 'accessing'!
  467. title
  468. ^'Tutorials'
  469. !
  470. contents
  471. ^'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)'
  472. !
  473. chapters
  474. ^{ self firstAppChapter. self counterChapter }
  475. !
  476. firstAppChapter
  477. ^DocChapter new
  478. title: 'A first application';
  479. contents: '
  480. Let''s make Hello World in Amber.
  481. First, you need a place for your new project. I made a new directory under amber:
  482. amber/projects/hello
  483. 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.
  484. 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:
  485. <!!DOCTYPE html>
  486. <html>
  487. <head>
  488. <title>My First Amber Project</title>
  489. <script src="../../js/amber.js" type="text/javascript"></script>
  490. <script type="text/javascript">
  491. loadAmber({
  492. files: [],
  493. prefix: ''projects/hello/js'',
  494. ready: function() {
  495. }});
  496. </script>
  497. </head>
  498. <body>
  499. <article>
  500. <h1>My First Amber Project</h1>
  501. <button onclick="smalltalk.Browser._open()">class browser</button>
  502. <button id="sayHello">say hello</button>
  503. </article>
  504. </body>
  505. </html>
  506. Now start up amber with node.js and navigate to http://localhost:4000/projects/hello/index.html
  507. 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.
  508. 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:
  509. Object subclass: #Hello
  510. instanceVariableNames: ''''
  511. package: ''HelloApp''
  512. Now click save and navigate to your new class in its new package.
  513. Then click ''commit package''. You just created a new class and saved your work.
  514. On your file system check out your js and st folders. Your new class is now saved in both JavaScript and Smalltalk.
  515. 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:
  516. loadAmber({
  517. files: [''HelloApp.js''],
  518. prefix: ''projects/hello/js'',
  519. ready: function() {
  520. }});
  521. Save and refresh again. Now your class is loaded and shows up in the class browser.
  522. 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:
  523. begin
  524. "Makes me say hello to the user."
  525. | msg button |
  526. msg := ''Hello world!!''.
  527. button := ''#sayHello'' asJQuery.
  528. button click: [button after: ''<p>'' , msg , ''</p>''].
  529. 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:
  530. loadAmber({
  531. files: [''HelloApp.js''],
  532. prefix: ''projects/hello/js'', // path for js files i think
  533. ready: function() {
  534. $(function() {
  535. smalltalk.Hello._new()._begin();
  536. });
  537. }});
  538. From there, you can create new Smalltalk classes and messages to build up your app. Enjoy!!
  539. '
  540. !
  541. counterChapter
  542. ^DocChapter new
  543. title: 'The counter application';
  544. contents: '
  545. This tutorial will teach you how to build HTML with Amber using jQuery and the HTMLCanvas API. It is freely adapted from
  546. the [Seaside counter example](http://www.seaside.st/about/examples/counter)
  547. ##The counter widget
  548. The counter is the most basic example of a widget. It allows to increment and decrement a number by clicking a button.
  549. Amber already comes with a counter example in the `Examples` package. To avoid class name conflict, we''ll name our counter class `TCounter`.
  550. Widget subclass: #TCounter
  551. instanceVariableNames: ''count header''
  552. package: ''Tutorials''
  553. The first method is used to initialize the component with the default state, in this case we set the counter to 0:
  554. initialize
  555. super initialize.
  556. count := 0
  557. The method used for rendering a widget is `#renderOn:`. It takes an instance of HTMLCanvas as parameter.
  558. The `header` h1 kept as an instance variable, so when the count value change, we can update it''s contents accordingly.
  559. renderOn: html
  560. header := html h1
  561. with: count asString;
  562. yourself.
  563. html button
  564. with: ''++'';
  565. onClick: [self increase].
  566. html button
  567. with: ''--'';
  568. onClick: [self decrease]
  569. The counter is almost ready. All we need now is to implement the two action methods `#increase` and `#decrease` to change the state
  570. of our counter and update its header.
  571. increase
  572. count := count + 1.
  573. header contents: [:html | html with: count asString]
  574. decrease
  575. count := count - 1.
  576. header contents: [:html | html with: count asString]
  577. That''s it!! We can now display an instance of TCounter by rendering it on the page using jQuery:
  578. TCounter new appendToJQuery: ''body'' asJQuery
  579. '
  580. ! !