suppression de vieux articles
This commit is contained in:
parent
b9f8ebf2dc
commit
2c0bfbd5e8
8 changed files with 0 additions and 656 deletions
|
@ -1,34 +0,0 @@
|
||||||
-----
|
|
||||||
|
|
||||||
# Custom
|
|
||||||
kind: article
|
|
||||||
menupriority: 1
|
|
||||||
title: MVC explained
|
|
||||||
multiTitle:
|
|
||||||
fr: design naïf
|
|
||||||
en: Naive design
|
|
||||||
|
|
||||||
-----
|
|
||||||
|
|
||||||
## The naive design ##
|
|
||||||
|
|
||||||
Suppose you want to make a simple todo list application.
|
|
||||||
In your mind you draw a simple scheme:
|
|
||||||
|
|
||||||
<pre>
|
|
||||||
[ ] task 1 X
|
|
||||||
[ ] task 2 X
|
|
||||||
[ ] task 3 X
|
|
||||||
</pre>
|
|
||||||
|
|
||||||
If you click on the box, then it should show the task is done.
|
|
||||||
If you click on the cross, then it should delete the task from
|
|
||||||
the list.
|
|
||||||
|
|
||||||
You then think:
|
|
||||||
> "Whoa! That's easy!"
|
|
||||||
|
|
||||||
You want to be able to use it as fast as possible
|
|
||||||
and you also want to complexify this model later.
|
|
||||||
|
|
||||||
For example, adding dates, contexts and projects...
|
|
|
@ -1,56 +0,0 @@
|
||||||
-----
|
|
||||||
# Built-in
|
|
||||||
filters_pre:
|
|
||||||
- "bluecloth"
|
|
||||||
|
|
||||||
# Custom
|
|
||||||
kind: article
|
|
||||||
menupriority: 2
|
|
||||||
title: MVC explained (Let's code it)
|
|
||||||
multiTitle:
|
|
||||||
fr: let's code it
|
|
||||||
en: Let's code it
|
|
||||||
|
|
||||||
-----
|
|
||||||
|
|
||||||
the purpose of this part is to code the webserver part of your
|
|
||||||
application. Some really complete framework already exists for that.
|
|
||||||
|
|
||||||
newcorps
|
|
||||||
|
|
||||||
## Let's code it ##
|
|
||||||
|
|
||||||
You choose a programming language (for readability I choosen
|
|
||||||
Ruby).
|
|
||||||
|
|
||||||
First you use a simple HTTP Server library (for simplicity
|
|
||||||
I choosen webrick)
|
|
||||||
|
|
||||||
Here an example of a very minimal HTTP Server. When it receives
|
|
||||||
a GET request at the port number 2000 at the directory '/' it returns:
|
|
||||||
|
|
||||||
<code class="html"><html>Content</html></code>
|
|
||||||
|
|
||||||
Here is the code:
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<code class="ruby" file="minimalHttpServer.rb">
|
|
||||||
#!/usr/bin/env ruby
|
|
||||||
require 'webrick'
|
|
||||||
include WEBrick
|
|
||||||
s = HTTPServer.new( :Port => 2000 )
|
|
||||||
|
|
||||||
class HelloServlet < HTTPServlet::AbstractServlet
|
|
||||||
def do_GET(req, res)
|
|
||||||
res.body = '<html>Content</html>'
|
|
||||||
res['Content-Type'] = "text/html"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
s.mount("/", HelloServlet)
|
|
||||||
s.start </code>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
You can try it yourself starting by downloading and starting the ruby program and clicking this [link (http://localhost:2000)](http://localhost:2000/) (You must start the server for this link to work).
|
|
||||||
|
|
||||||
I Believe what it does is mostly straightforward
|
|
|
@ -1,105 +0,0 @@
|
||||||
-----
|
|
||||||
# Built-in
|
|
||||||
filters_pre:
|
|
||||||
- "bluecloth"
|
|
||||||
|
|
||||||
# Custom
|
|
||||||
kind: article
|
|
||||||
menupriority: 3
|
|
||||||
title: MVC explained (first design cleaning)
|
|
||||||
multiTitle:
|
|
||||||
fr: first design cleaning
|
|
||||||
en: first design cleaning
|
|
||||||
|
|
||||||
-----
|
|
||||||
|
|
||||||
## first design cleaning ##
|
|
||||||
|
|
||||||
If you want your applications to
|
|
||||||
generate strict XHTML 1.0 web pages you
|
|
||||||
have to modify your application to put the content
|
|
||||||
between an *header* and a *footer*.
|
|
||||||
|
|
||||||
Naturally, You write directly the header and footer
|
|
||||||
in the source code of the application.
|
|
||||||
|
|
||||||
You also want to initialize with some tasks.
|
|
||||||
And in order to prepare the future, you separate
|
|
||||||
the server code from you todo list code.
|
|
||||||
Then you write a specifical class for the two
|
|
||||||
*features* of your application.
|
|
||||||
|
|
||||||
Your new code will look like:
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<code class="ruby">
|
|
||||||
class Todo
|
|
||||||
def content
|
|
||||||
Print the content of the list
|
|
||||||
...
|
|
||||||
|
|
||||||
todo = new Todo
|
|
||||||
HEADER="<... code HTML ...>"
|
|
||||||
FOOTER="<... code HTML ...>"
|
|
||||||
|
|
||||||
Class Server
|
|
||||||
def do_GET
|
|
||||||
return HEADER + (todo.content) + FOOTER
|
|
||||||
|
|
||||||
server = new Server
|
|
||||||
server.start # and for each GET request execute do_GET</code>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
Here are the details of the modified code:
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<code class="ruby" file="step0.rb">
|
|
||||||
#!/usr/bin/env ruby
|
|
||||||
|
|
||||||
require 'webrick'
|
|
||||||
include WEBrick
|
|
||||||
|
|
||||||
s = HTTPServer.new( :Port => 2000 )
|
|
||||||
|
|
||||||
class Todo
|
|
||||||
def initialize
|
|
||||||
@todoList=['first task','second task']
|
|
||||||
end
|
|
||||||
def content
|
|
||||||
res='<h1>Todo</h1><ul>'
|
|
||||||
@todoList.each{|task| res<<='<li>'+task+'</li>'}
|
|
||||||
res<<='</ul>'
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Servlet
|
|
||||||
|
|
||||||
# you write the common header and footer of all pages
|
|
||||||
# directly in the source code
|
|
||||||
@@Header='<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
|
||||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr">
|
|
||||||
<head>
|
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
|
||||||
<title>Todo Application</title>
|
|
||||||
</head>
|
|
||||||
<body><div id="content">'
|
|
||||||
@@Footer='</div></body>'
|
|
||||||
@@Todo=Todo.new
|
|
||||||
|
|
||||||
def do_GET(req, res)
|
|
||||||
res.body = @@Header + @@Todo.content + @@Footer
|
|
||||||
res['Content-Type'] = "text/html"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
servlet=Servlet.new
|
|
||||||
s.mount("/"){ |req,res| server.do_GET(req,res) }
|
|
||||||
trap("INT"){ s.shutdown }
|
|
||||||
s.start </code>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
For now your application isn't functionnal. But it's great,
|
|
||||||
you can print your todo list. We just have to add content
|
|
||||||
at runtime now.
|
|
|
@ -1,133 +0,0 @@
|
||||||
-----
|
|
||||||
# Built-in
|
|
||||||
filters_pre:
|
|
||||||
- "bluecloth"
|
|
||||||
|
|
||||||
# Custom
|
|
||||||
kind: article
|
|
||||||
menupriority: 4
|
|
||||||
title: MVC explained (make it usable)
|
|
||||||
multiTitle:
|
|
||||||
fr: make it usable
|
|
||||||
en: make it usable
|
|
||||||
|
|
||||||
-----
|
|
||||||
|
|
||||||
## Make it usable ##
|
|
||||||
|
|
||||||
The app is almost usable. You only need to have a way to
|
|
||||||
add new tasks and to delete them. Let's do it using
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<code class="html">
|
|
||||||
http://localhost:2000/?newtask="new task text"
|
|
||||||
http://localhost:2000/?deletetask=taskNumber </code>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
It is not a so difficult part, you only need a little
|
|
||||||
code in the `do_GET` function of server to call to correct
|
|
||||||
Todo methods depending of the request.
|
|
||||||
|
|
||||||
And of course code the correponding method in the Todo class.
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<code class="ruby" file="naivetodolist.rb">
|
|
||||||
#!/usr/bin/env ruby
|
|
||||||
|
|
||||||
require 'webrick'
|
|
||||||
# the Todo class is in an external file
|
|
||||||
require 'todo.rb'
|
|
||||||
include WEBrick
|
|
||||||
|
|
||||||
s = HTTPServer.new( :Port => 2000 )
|
|
||||||
|
|
||||||
class Servlet
|
|
||||||
# you write the common header and footer of all pages
|
|
||||||
# directly in the source code
|
|
||||||
@@Header='<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
|
||||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
|
||||||
lang="fr" xml:lang="fr">
|
|
||||||
<head>
|
|
||||||
<meta http-equiv="Content-Type"
|
|
||||||
content="text/html; charset=UTF-8" />
|
|
||||||
<title>Todo Application</title>
|
|
||||||
</head>
|
|
||||||
<body><div id="content">'
|
|
||||||
@@Footer='</div></body>'
|
|
||||||
|
|
||||||
def initialize()
|
|
||||||
@todo=Todo.new
|
|
||||||
end
|
|
||||||
|
|
||||||
# implement the GET request
|
|
||||||
def do_GET(req, res)
|
|
||||||
if req.query['newtask']
|
|
||||||
@todo.addTask(req.query['newtask'])
|
|
||||||
elsif req.query['deletetask']
|
|
||||||
@todo.deleteTask(req.query['deletetask'])
|
|
||||||
end
|
|
||||||
res.body = @@Header + @todo.content + @@Footer
|
|
||||||
res['Content-Type'] = "text/html"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
servlet=Servlet.new
|
|
||||||
s.mount_proc("/"){ |req,res| servlet.do_GET(req,res) }
|
|
||||||
|
|
||||||
trap("INT"){ s.shutdown }
|
|
||||||
s.start</code>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
Where the TODO class is really simple and coded like this:
|
|
||||||
|
|
||||||
<div>
|
|
||||||
|
|
||||||
<code class="ruby" file="todo.rb">
|
|
||||||
class Todo
|
|
||||||
def initialize
|
|
||||||
@todoList=['first task','second task']
|
|
||||||
end
|
|
||||||
def content
|
|
||||||
res='<h1>Todo</h1><ul>';
|
|
||||||
i=0
|
|
||||||
@todoList.each do |task|
|
|
||||||
res<<='<li>'+task+
|
|
||||||
' <a href="?deletetask='+ i.to_s +
|
|
||||||
'">done</a>'+'</li>'
|
|
||||||
i+=1
|
|
||||||
end
|
|
||||||
res<<='</ul>
|
|
||||||
<form name="input" action="#" metho="get">
|
|
||||||
Task: <input type="text" name="newtask"/>
|
|
||||||
</form>'
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
def addTask(task)
|
|
||||||
@todoList<<=task
|
|
||||||
end
|
|
||||||
def deleteTask(taskNum)
|
|
||||||
@todoList.delete_at(Integer(taskNum.to_s))
|
|
||||||
end
|
|
||||||
|
|
||||||
end</code>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
Here is the results :
|
|
||||||
|
|
||||||
![A screenshot of this minimal application](/Scratch/img/MVCWhy/Screenshot_v0.png "Screenshot naive version")
|
|
||||||
|
|
||||||
Now we have separated the server and the todo list into
|
|
||||||
two different files.
|
|
||||||
|
|
||||||
The application's working within only 76 lines of code
|
|
||||||
|
|
||||||
Now speak about the problems of this (working) implementation.
|
|
||||||
|
|
||||||
What will occurs if you want to change from list to tables for
|
|
||||||
example or more generally if we want to change the html structure?
|
|
||||||
|
|
||||||
You'll have to modify the **Todo** class.
|
|
||||||
|
|
||||||
This is why you should separate even more each concepts.
|
|
|
@ -1,34 +0,0 @@
|
||||||
-----
|
|
||||||
|
|
||||||
# Custom
|
|
||||||
kind: article
|
|
||||||
menupriority: 1
|
|
||||||
title: MVC explained
|
|
||||||
multiTitle:
|
|
||||||
fr: design naïf
|
|
||||||
en: Naive design
|
|
||||||
|
|
||||||
-----
|
|
||||||
|
|
||||||
## The naive design ##
|
|
||||||
|
|
||||||
Suppose you want to make a simple todo list application.
|
|
||||||
In your mind you draw a simple scheme:
|
|
||||||
|
|
||||||
<pre>
|
|
||||||
[ ] task 1 X
|
|
||||||
[ ] task 2 X
|
|
||||||
[ ] task 3 X
|
|
||||||
</pre>
|
|
||||||
|
|
||||||
If you click on the box, then it should show the task is done.
|
|
||||||
If you click on the cross, then it should delete the task from
|
|
||||||
the list.
|
|
||||||
|
|
||||||
You then think:
|
|
||||||
> "Whoa! That's easy!"
|
|
||||||
|
|
||||||
You want to be able to use it as fast as possible
|
|
||||||
and you also want to complexify this model later.
|
|
||||||
|
|
||||||
For example, adding dates, contexts and projects...
|
|
|
@ -1,56 +0,0 @@
|
||||||
-----
|
|
||||||
# Built-in
|
|
||||||
filters_pre:
|
|
||||||
- "bluecloth"
|
|
||||||
|
|
||||||
# Custom
|
|
||||||
kind: article
|
|
||||||
menupriority: 2
|
|
||||||
title: MVC explained (Let's code it)
|
|
||||||
multiTitle:
|
|
||||||
fr: let's code it
|
|
||||||
en: Let's code it
|
|
||||||
|
|
||||||
-----
|
|
||||||
|
|
||||||
the purpose of this part is to code the webserver part of your
|
|
||||||
application. Some really complete framework already exists for that.
|
|
||||||
|
|
||||||
newcorps
|
|
||||||
|
|
||||||
## Let's code it ##
|
|
||||||
|
|
||||||
You choose a programming language (for readability I choosen
|
|
||||||
Ruby).
|
|
||||||
|
|
||||||
First you use a simple HTTP Server library (for simplicity
|
|
||||||
I choosen webrick)
|
|
||||||
|
|
||||||
Here an example of a very minimal HTTP Server. When it receives
|
|
||||||
a GET request at the port number 2000 at the directory '/' it returns:
|
|
||||||
|
|
||||||
<code class="html"><html>Content</html></code>
|
|
||||||
|
|
||||||
Here is the code:
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<code class="ruby" file="minimalHttpServer.rb">
|
|
||||||
#!/usr/bin/env ruby
|
|
||||||
require 'webrick'
|
|
||||||
include WEBrick
|
|
||||||
s = HTTPServer.new( :Port => 2000 )
|
|
||||||
|
|
||||||
class HelloServlet < HTTPServlet::AbstractServlet
|
|
||||||
def do_GET(req, res)
|
|
||||||
res.body = '<html>Content</html>'
|
|
||||||
res['Content-Type'] = "text/html"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
s.mount("/", HelloServlet)
|
|
||||||
s.start </code>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
You can try it yourself starting by downloading and starting the ruby program and clicking this [link (http://localhost:2000)](http://localhost:2000/) (You must start the server for this link to work).
|
|
||||||
|
|
||||||
I Believe what it does is mostly straightforward
|
|
|
@ -1,105 +0,0 @@
|
||||||
-----
|
|
||||||
# Built-in
|
|
||||||
filters_pre:
|
|
||||||
- "bluecloth"
|
|
||||||
|
|
||||||
# Custom
|
|
||||||
kind: article
|
|
||||||
menupriority: 3
|
|
||||||
title: MVC explained (first design cleaning)
|
|
||||||
multiTitle:
|
|
||||||
fr: first design cleaning
|
|
||||||
en: first design cleaning
|
|
||||||
|
|
||||||
-----
|
|
||||||
|
|
||||||
## first design cleaning ##
|
|
||||||
|
|
||||||
If you want your applications to
|
|
||||||
generate strict XHTML 1.0 web pages you
|
|
||||||
have to modify your application to put the content
|
|
||||||
between an *header* and a *footer*.
|
|
||||||
|
|
||||||
Naturally, You write directly the header and footer
|
|
||||||
in the source code of the application.
|
|
||||||
|
|
||||||
You also want to initialize with some tasks.
|
|
||||||
And in order to prepare the future, you separate
|
|
||||||
the server code from you todo list code.
|
|
||||||
Then you write a specifical class for the two
|
|
||||||
*features* of your application.
|
|
||||||
|
|
||||||
Your new code will look like:
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<code class="ruby">
|
|
||||||
class Todo
|
|
||||||
def content
|
|
||||||
Print the content of the list
|
|
||||||
...
|
|
||||||
|
|
||||||
todo = new Todo
|
|
||||||
HEADER="<... code HTML ...>"
|
|
||||||
FOOTER="<... code HTML ...>"
|
|
||||||
|
|
||||||
Class Server
|
|
||||||
def do_GET
|
|
||||||
return HEADER + (todo.content) + FOOTER
|
|
||||||
|
|
||||||
server = new Server
|
|
||||||
server.start # and for each GET request execute do_GET</code>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
Here are the details of the modified code:
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<code class="ruby" file="step0.rb">
|
|
||||||
#!/usr/bin/env ruby
|
|
||||||
|
|
||||||
require 'webrick'
|
|
||||||
include WEBrick
|
|
||||||
|
|
||||||
s = HTTPServer.new( :Port => 2000 )
|
|
||||||
|
|
||||||
class Todo
|
|
||||||
def initialize
|
|
||||||
@todoList=['first task','second task']
|
|
||||||
end
|
|
||||||
def content
|
|
||||||
res='<h1>Todo</h1><ul>'
|
|
||||||
@todoList.each{|task| res<<='<li>'+task+'</li>'}
|
|
||||||
res<<='</ul>'
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Servlet
|
|
||||||
|
|
||||||
# you write the common header and footer of all pages
|
|
||||||
# directly in the source code
|
|
||||||
@@Header='<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
|
||||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr">
|
|
||||||
<head>
|
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
|
||||||
<title>Todo Application</title>
|
|
||||||
</head>
|
|
||||||
<body><div id="content">'
|
|
||||||
@@Footer='</div></body>'
|
|
||||||
@@Todo=Todo.new
|
|
||||||
|
|
||||||
def do_GET(req, res)
|
|
||||||
res.body = @@Header + @@Todo.content + @@Footer
|
|
||||||
res['Content-Type'] = "text/html"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
servlet=Servlet.new
|
|
||||||
s.mount("/"){ |req,res| server.do_GET(req,res) }
|
|
||||||
trap("INT"){ s.shutdown }
|
|
||||||
s.start </code>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
For now your application isn't functionnal. But it's great,
|
|
||||||
you can print your todo list. We just have to add content
|
|
||||||
at runtime now.
|
|
|
@ -1,133 +0,0 @@
|
||||||
-----
|
|
||||||
# Built-in
|
|
||||||
filters_pre:
|
|
||||||
- "bluecloth"
|
|
||||||
|
|
||||||
# Custom
|
|
||||||
kind: article
|
|
||||||
menupriority: 4
|
|
||||||
title: MVC explained (make it usable)
|
|
||||||
multiTitle:
|
|
||||||
fr: make it usable
|
|
||||||
en: make it usable
|
|
||||||
|
|
||||||
-----
|
|
||||||
|
|
||||||
## Make it usable ##
|
|
||||||
|
|
||||||
The app is almost usable. You only need to have a way to
|
|
||||||
add new tasks and to delete them. Let's do it using
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<code class="html">
|
|
||||||
http://localhost:2000/?newtask="new task text"
|
|
||||||
http://localhost:2000/?deletetask=taskNumber </code>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
It is not a so difficult part, you only need a little
|
|
||||||
code in the `do_GET` function of server to call to correct
|
|
||||||
Todo methods depending of the request.
|
|
||||||
|
|
||||||
And of course code the correponding method in the Todo class.
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<code class="ruby" file="naivetodolist.rb">
|
|
||||||
#!/usr/bin/env ruby
|
|
||||||
|
|
||||||
require 'webrick'
|
|
||||||
# the Todo class is in an external file
|
|
||||||
require 'todo.rb'
|
|
||||||
include WEBrick
|
|
||||||
|
|
||||||
s = HTTPServer.new( :Port => 2000 )
|
|
||||||
|
|
||||||
class Servlet
|
|
||||||
# you write the common header and footer of all pages
|
|
||||||
# directly in the source code
|
|
||||||
@@Header='<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
|
||||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
|
||||||
lang="fr" xml:lang="fr">
|
|
||||||
<head>
|
|
||||||
<meta http-equiv="Content-Type"
|
|
||||||
content="text/html; charset=UTF-8" />
|
|
||||||
<title>Todo Application</title>
|
|
||||||
</head>
|
|
||||||
<body><div id="content">'
|
|
||||||
@@Footer='</div></body>'
|
|
||||||
|
|
||||||
def initialize()
|
|
||||||
@todo=Todo.new
|
|
||||||
end
|
|
||||||
|
|
||||||
# implement the GET request
|
|
||||||
def do_GET(req, res)
|
|
||||||
if req.query['newtask']
|
|
||||||
@todo.addTask(req.query['newtask'])
|
|
||||||
elsif req.query['deletetask']
|
|
||||||
@todo.deleteTask(req.query['deletetask'])
|
|
||||||
end
|
|
||||||
res.body = @@Header + @todo.content + @@Footer
|
|
||||||
res['Content-Type'] = "text/html"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
servlet=Servlet.new
|
|
||||||
s.mount_proc("/"){ |req,res| servlet.do_GET(req,res) }
|
|
||||||
|
|
||||||
trap("INT"){ s.shutdown }
|
|
||||||
s.start</code>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
Where the TODO class is really simple and coded like this:
|
|
||||||
|
|
||||||
<div>
|
|
||||||
|
|
||||||
<code class="ruby" file="todo.rb">
|
|
||||||
class Todo
|
|
||||||
def initialize
|
|
||||||
@todoList=['first task','second task']
|
|
||||||
end
|
|
||||||
def content
|
|
||||||
res='<h1>Todo</h1><ul>';
|
|
||||||
i=0
|
|
||||||
@todoList.each do |task|
|
|
||||||
res<<='<li>'+task+
|
|
||||||
' <a href="?deletetask='+ i.to_s +
|
|
||||||
'">done</a>'+'</li>'
|
|
||||||
i+=1
|
|
||||||
end
|
|
||||||
res<<='</ul>
|
|
||||||
<form name="input" action="#" metho="get">
|
|
||||||
Task: <input type="text" name="newtask"/>
|
|
||||||
</form>'
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
def addTask(task)
|
|
||||||
@todoList<<=task
|
|
||||||
end
|
|
||||||
def deleteTask(taskNum)
|
|
||||||
@todoList.delete_at(Integer(taskNum.to_s))
|
|
||||||
end
|
|
||||||
|
|
||||||
end</code>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
Here is the results :
|
|
||||||
|
|
||||||
![A screenshot of this minimal application](/Scratch/img/MVCWhy/Screenshot_v0.png "Screenshot naive version")
|
|
||||||
|
|
||||||
Now we have separated the server and the todo list into
|
|
||||||
two different files.
|
|
||||||
|
|
||||||
The application's working within only 76 lines of code
|
|
||||||
|
|
||||||
Now speak about the problems of this (working) implementation.
|
|
||||||
|
|
||||||
What will occurs if you want to change from list to tables for
|
|
||||||
example or more generally if we want to change the html structure?
|
|
||||||
|
|
||||||
You'll have to modify the **Todo** class.
|
|
||||||
|
|
||||||
This is why you should separate even more each concepts.
|
|
Loading…
Reference in a new issue