The functionality is quite simple
1) Display the Login Page
2) User enters username and password and clicks on validate button
3) If the credentials are validated (in this case username='smalltalk' ) a welcome message is displayed , otherwise login page is displayed again.
Quite simple.... lets see how intuitive it can be
Lets start with defining our Login Component as below
WAComponent subclass: #Login
instanceVariableNames: 'username password retry'
classVariableNames: ''
poolDictionaries: ''
category: 'LearnSeaside'!
this should be self explaining , We defined a class Login subclassing it from WAComponent.
It has three instance variables , I used "retry" to indicate if it is a retry or first attempt. (may not be the best way ..)
Seaside calls renderComponentOn method when it is appropriate to display the content on the web page. Let us look at how we want to display the content on page.
renderContentOn: html
html
form: [
retry ifTrue: [
html text: 'Retry..'.
html break].
html text: 'User Name'.
html textInput on: #username of: self.
html passwordInput on: #password of: self.
html submitButton callback: [self answer: self];
value: 'Validate']
Nothing special .. it just displays user name and password fields, in addition to that , it display text "Retry" based on the instance variable "retry".
Now lets add some magic. we will now write a Task to start this.
we will name our task WASchoolTask , subclassing WATask. it will have a method "go" which will be called by seaside as entry point to the application.
let us look at the go method.
go
| login |
login := self call: Login new.
[login username = 'smalltalk']
whileFalse: [login retry: true.
login := self call: login].
self inform: 'Welcome ' , login username
as you can see in the above code we want to display the login screen until the user enters username as smalltalk. What a great way to write your applications describing the natural flow of the application! Notice the statement
self inform:'Welcome ',login username
The best part of the code is that this statement will not be executed unless the loop is broken. Imagine how you would do this in a Action based framework.
No comments:
Post a Comment