30. 理解MVC
The Model-View-Controller architectural pattern
(MVC) divides an interactive application into three
components.
The model contains the core functionality and data.
Views display information to the user.
Controllers handle user input.
Views and controllers together comprise the user
interface.
A change-propagation mechanism ensures
consistency between the user interface and the model.
——POSA(面向模式的软件架构)
35. Ajax
Browser Client
Browser Client User Interface
User Interface Script call
HTML+CSS data
AJAX Engine
HTTP request
HTTP request
HTML + CSS data XML data
Web Server Web and/or XML Server
Data store,… Data store,…
Server Side System Server Side System
36. Ajax
似乎更复杂了?
Browser Client
Browser Client User Interface
User Interface Script call
HTML+CSS data
AJAX Engine
HTTP request
HTTP request
HTML + CSS data
XML
data
Web Server Web and/or XML Server
Data store,… Data store,…
Server Side System Server Side System
37. Ajax
似乎更复杂了?
Browser Client
Browser Client User Interface
User Interface Script call
HTML+CSS data
AJAX Engine
HTTP request
HTTP request
HTML + CSS data
XML
data
Web Server Web and/or XML Server
1.Page
2.Form
Data store,… 3.Action Data store,…
4.Persistent
Server Side System Objects Server Side System
38. Ajax
似乎更复杂了?
Browser Client
Browser Client User Interface
User Interface Script call
HTML+CSS data
AJAX Engine
HTTP request
HTTP request
HTML + CSS data
XML
data
Web Server Web and/or XML Server
Data store,…
服务化 Data store,…
Server Side System Server Side System
39. 实现Ajax
• 交互前端化(extjs)
tbar:[ {
x ty pe:' bu tton' ,
tex t:'清空所选择的机器' ,
han dl er:func tion () {ch oo seMach in eToE xecu te.eac h (
fu nctio n(rec ord) {
reco rd .set('c ho oseToE x ecu te', fa lse);
}
); }
}...]
43. 举例1
• 状态迁移的逻辑?
//d irec ti ve.rb ( by event_ mac hin )
sta te_ mach in e :sta te, :in itial => :i ni t do
even t :cl ea r do tra n si ti on [:in it, :read y] => :do ne en d
even t :ack do tra nsitio n :f ail ure => :d on e end
end
44. 举例1
• 状态迁移的逻辑?
//d irec ti ve.rb ( by event_ mac hin )
sta te_ mach in e :sta te, :in itial => :i ni t do
even t :cl ea r do tra n si ti on [:in it, :read y] => :do ne en d
even t :ack do tra nsitio n :f ail ure => :d on e end
end
写两个版本(js, ruby)么?
49. 我们的思路
// appl ica ti on .j s
$('d iv[ bo x-href] ,li[ bo x-href] ').li ve(
' ref resh .a pplica tion' ,
fun ction (e) {
var ba se_ no de = $ (e.cu rrentTa rget);
a ppl ica ti on .refresh (
b ase_ nod e,
a pplic ation .li nk(ba se_ nod e)
);
retu rn a pplic ation .sto pEvery thi ng (e);
}
);
50. 我们的思路
// appl ica ti on .j s
……
l in k: f unc ti on (n od e){
if (nod e.data ('bo x -href ')==nu ll ){
n od e.da ta ("b ox -h ref",n od e.attr('b ox -h ref' ));
}
retu rn n od e.da ta (' bo x-h ref' );
},
……
52. 举例1
// o pera ti on s/_c ol lection .html .erb
<% opera ti on s.wi tho ut_ state(:do n e).ea ch do |o | %>
<l i bo x-href= "<% =o pera ti on_ path(o .i d) %> ">
<% =ren d er ' operatio ns/i tem', :resou rc e => o %>
</l i>
<% end %>
//d irec ti ves/_i tem .h tm l .erb
li nk_ to ('清理 ', cl ea r_ operatio n_pa th (o .id ),
:remo te => tru e, :m ethod => :put, :han dl e => tru e
)
53. 举例1
//a pplica ti on .j s
$('a[ha ndl e]' ).li ve('aj ax :succ ess', f unc ti on (e, da ta ,
sta tu s, x hr) {
var n od e = $ (e.cu rren tTa rg et).pa ren t();
var pa rent =
appl ica ti on .u pto (no d e,' di v[b ox -h ref],l i[b ox -h ref]' );
if (pa rent){
paren t.trig ger("ref resh .appl ica ti on ");
}
retu rn a ppl ica ti on .stopE very thi ng (e);
});
54. 举例1
// d irec ti ves/_co ll ec ti on .h tm l .erb
<% di rec ti ves.ea ch do |d irec ti ve|%>
<l i bo x-href= "<% =di rec ti ve_pa th (d i rective.id ) %> ">
<%= rend er 'di rec ti ves/i tem' , :resou rc e =>
di rec ti ve %>
</l i>
<% end %>
//d irec ti ves/_i tem .h tm l .erb
li nk_ to ('清理 ', cl ea r_ di rective_ pa th (o .id ),
:remo te => tru e, :m ethod => :put, :han dl e => tru e
)
56. 举例2
// ma chi nes/_ col l ectio n .html .erb
li nk_ to ('暂停 ',
pa use_ mach in e_path (mach in e.i d ),
:rem ote => tru e, :m etho d => :pu t )
// ma chi nes/ pau se.j s.erb
al ert('生成<% =@di recti ve.co mma nd_n am e %>指令' );
$("#ma chi nes").h tml ('<%= esc ape_j avascript(rend er
"/ mac hin es/c ol lection ", :a pp = > @mach in e.a pp) %>' );
57. 举例2
// appl ica ti on .j s
$('select[ box -remo te]' ).l ive(' chan g e',f unc ti on (e){
var el em ent = $ (thi s);
var u rl = el ement.a ttr(' url' ),
meth od = el em en t.attr('m etho d' ),
opti on = th i s.o ptio ns[ th is.sel ec ted In dex],
nam e = th i s.na me;
var d ata = n am e + "= " + optio n .val ue;
$ .aj ax ({u rl : u rl, ty pe: m etho d || ' GE T', data: data });
retu rn a ppl ica ti on .stopE very thi ng (e);
});