找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 2124|回复: 3

[webOS开发] UI Widget List: ShowDialog() (Custom Dialog)

[复制链接]
发表于 2010-4-14 13:48 | 显示全部楼层 |阅读模式
ShowDialog() (Custom Dialog) 2 }/ M/ E% _) ?
Example usage:
  1. // addNewFeed - triggered by "Add..." item in feed list and invokes the AddDialog. i4 _- c/ Y! W5 y+ @* I* |. p4 R
  2. // Assistant defined above./ k+ [( W* q8 E6 c
  3. //
    ! }0 B: Q* ^2 b* m5 Y5 m+ t
  4. FeedListAssistant.prototype.addNewFeed = function() {5 n' q& x! S; R3 k9 T
  5.   this.controller.showDialog({5 m% `: d6 s, X# N4 J
  6.     template: 'feedList/addFeed-dialog',0 U% p2 L, m+ [; K( K# x) ?, C
  7.     assistant: new AddDialogAssistant(this)
    6 Q0 q% G5 i6 b
  8.   });6 K! e7 h$ G6 A2 `. s( p
  9. };
复制代码

9 _( D6 N# h9 ]+ m( _" T- _* b/ ?
4 w  s! E% y$ n; l8 }- PA custom dialog uses a template (HTML file) and an assistant (JavaScript file), similar to a scene.  The HTML file defines what will be displayed in the custom dialog and the JavaScript file defines how the elements will behave.$ V* V. c; o4 C  x1 _

) D3 ~2 C4 G4 Y; c! f4 d8 MTemplate file:
  1. <div>
      O1 R8 D& o4 K  u
  2.   <span id="add-feed-title" class="palm-dialog-title">Add News Feed Source</span>1 M4 R% |5 k" W9 k/ S, P
  3.   <div class="palm-group unlabeled">. E. O4 h: \+ i% a9 Z: C" P, s) K# P
  4.     <div class="palm-list">3 c/ r. N! H  \( Z
  5.       <div class='palm-row first'>  W# I* K( B0 X- d+ U
  6.         <div class="palm-row-wrapper textfield-group" x-mojo-focus-highlight="true">
    6 n0 b; |) F0 e4 L" d+ r9 W1 O2 |
  7.           <div class="title">2 [  r, p" c& e, v2 u! f8 ]: D
  8.             <div class="label">URL</div># f) W  B: g8 m6 ?6 C, C
  9.             <div id="newFeedURL" x-mojo-element="TextField" align="left"></div>9 X& U2 `: O# W. l
  10.           </div>$ S3 s8 z7 V- B
  11.         </div>
    8 o& s& A6 E- e6 A- S1 g, S# ]4 n0 T
  12.       </div>6 A* A5 b, _* z, p
  13.       <div class='palm-row last'>
    6 A. U, C7 k/ `2 L& b% @
  14.         <div class="palm-row-wrapper textfield-group" x-mojo-focus-highlight="true">
    4 `5 p2 Q( p) h( d# a9 p
  15.           <div class="title">, a4 s$ Y7 \& {" X0 u+ n9 m: g
  16.             <div class="label">Title</div>
    0 {) n4 c  v" T6 a$ ?
  17.             <div id="newFeedName" x-mojo-element="TextField" align="left"></div>
    0 n5 ~. a: Z& L$ T
  18.           </div>. M9 l, v3 d5 t. h, g8 _
  19.         </div>
    # p7 V+ N3 v6 b5 n6 R3 L7 }! e+ h
  20.       </div>, H+ J& u! T- m* V" A6 q
  21.     </div>+ f+ H/ X2 p1 u, Z8 j4 v7 p
  22.   </div>
    . A: h3 O# |( }3 r
  23.   <div x-mojo-element="Button" id="okButton"></div>
    4 e- }4 |. l+ }7 j
  24. </div>
复制代码
Here is the assistant for the dialog, which sets up the various widgets as well as the event handlers that will respond to user interaction with the widgets.
9 }; H# V1 l  w7 h
" C2 B7 D1 v. ]Assistant file:
  1. // AddDialogAssistant - simple controller for adding new feeds to the list.
    * v. Z6 E9 ?6 e) @* H
  2. // Invoked by the FeedListAssistant when the "Add..." list item is selected4 E8 H4 D5 V5 j( o' W
  3. // on the feed list.
    . A4 f  s& p1 _* \7 G
  4. //
    4 u8 @6 N! x7 G4 g) [, `
  5. // The dialog displays two text fields (URL and Name) and an OK button.* Z+ I) j  t' d7 \$ I
  6. // Either the user enters a feed URL and a name, followed by OK or a; T7 b2 V2 K5 J. F2 B$ V: O* ?3 X
  7. // back swipe to close. If OK, the feed header is checked through an Ajax
    ( N) r" C2 b& Y/ n) U
  8. // request and if valid, the feed updated and dialog closed. If an error,
    ! S9 ]9 f" N" S! i7 k7 r( U
  9. // the posted in place of title and dialog remains open.
    ) D' k& |( w& g- y. ]
  10. // Swipe back cancels and returns back to the FeedListAssistant.  n  ~1 H+ g9 H: e5 S
  11. //# s6 B! _' t1 [
  12. function AddDialogAssistant(sceneAssistant) {; s1 j+ b; U  T! R2 T; F3 u
  13.   this.sceneAssistant = sceneAssistant;4 o( y' ]3 T3 K6 \9 J% r
  14. }
    9 t# M/ i0 d' l' x
  15. AddDialogAssistant.prototype.setup = function(widget) {
    8 ~3 H- ?: w5 z
  16.   this.widget = widget;
    3 y! t# \0 o$ m( f' y7 v
  17.   // Setup text field for the new feed's URL3 N9 W$ c+ t  g
  18.   //: ]  y) |8 d- o( O8 Z
  19.   this.sceneAssistant.controller.setupWidget("newFeedURL",# [3 q; R' R' B6 x3 T% j
  20.     this.urlAttributes = {
      J5 J- x$ Q3 @
  21.       property: "value",
    " G. d5 G8 y) Q2 X0 W- \! J  z
  22.       hintText: "RSS or ATOM feed",& ]3 j4 ?$ {4 u" t
  23.       focus: true,
    & ]1 p, ?6 Q* _% [; e& D
  24.       limitResize: true,! H8 [' h1 I9 C+ B$ g2 X
  25.       textReplacement: false,8 N1 l6 N5 g6 w9 u8 c% ^; b) F
  26.       enterSubmits: false* u4 a. v. g; f( U' L+ M
  27.     },) r& U/ F" n& y4 O2 E" i7 h
  28.     this.urlModel = {value : ""}
    2 |" D" E2 w7 B, j
  29.   );( I* S0 i/ [2 Y4 x+ h
  30.   // Setup text field for the new feed's name2 h& c5 h0 ]8 P5 v( A7 s
  31.   //& C! T; r* N0 x: u+ O
  32.   this.sceneAssistant.controller.setupWidget("newFeedName",
    + r( l, N0 z1 H' K0 j
  33.     this.nameAttributes = {5 L0 o1 P6 x4 h
  34.       property: "value",
    2 [1 c: r% v4 `  T" G  R$ l
  35.       hintText: "Optional",
    + C( M* q4 H4 f! [2 o8 H
  36.       limitResize: true,
    : N4 d* l# @4 f( r) p& }
  37.       textReplacement: false,
    $ K# K  [$ ~( Y, _
  38.       enterSubmits: false
    7 Y0 X6 {( n! |2 J9 z
  39.     },/ G1 ^1 K2 m6 W+ G. Y
  40.     this.nameModel = {value : ""
    7 L( e; Z* I, l( n7 A7 l
  41.   });" F6 _/ N/ C& b( s( ^/ W/ v
  42.   // Setup button and event handler- }  D% p+ b4 G0 Z; x: S
  43.   /// |; b# n- T& Q* h0 F  w
  44.   this.sceneAssistant.controller.setupWidget("okButton",3 D+ v0 a4 P+ T4 a" T; k
  45.     this.attributes = {},- X$ _% ^! H1 |4 ]) G- p$ t
  46.     this.model = {
    1 N. z* m  m" F# j% _. K
  47.       buttonLabel: "Add Feed",( q: B- ^! ]9 t1 q/ W0 m4 g
  48.       buttonClass: "addFeedButton",6 ~+ b+ x7 u0 s- L" |; N+ c& _
  49.       disabled: false
    ; Y! [, O! `0 J9 }4 D1 P
  50.     }
    # w+ \  r0 g- P/ X
  51.   );) ]" [% Z4 Q7 E; P! \& M. t* z
  52.   Mojo.Event.listen($('okButton'), Mojo.Event.tap, this.checkIt.bindAsEventListener(this));
    3 \* K! X5 k* d- b6 x  x
  53. };
    ' l6 |- @  M2 x- S8 s/ H- o
  54. // ------------------------------------------------------------------------
    4 \" c  F' _8 A4 v. T+ `
  55. // Add Feed Functions  Q5 I% @* B6 |. j  t7 a6 a
  56. //5 Y: c: Q1 Y: a( q7 K  r; @
  57. // checkIt - called when Add feed OK button is clicked
    . X) K9 T3 P; ^4 k' I, h0 T" `
  58. //$ D9 z/ {& h& |: J6 C1 Q# }2 u, q2 I
  59. AddDialogAssistant.prototype.checkIt = function() {* ]  k4 |' k7 {  C( r5 M, z8 ^
  60.   // Check for 'http://' on front or other legal prefix( J; m0 x5 H1 b1 T/ j
  61.   // assume any string of 1 to 5 alpha characters followed+ R& g; r! u! `( v3 [/ u$ |  d1 [8 y
  62.   // by ':' is legal, otherwise prepend "http://"/ i9 o5 ]% P* \0 `' h
  63.   //  H4 \, n* o- \
  64.   var url = this.urlModel.value;
    & o  P1 e$ K5 L  U* h* R. Q
  65.   if (/^[a-z]{1,5}:/.test(url) === false) {
    . i. Q) Y1 w* [; l: y+ k9 {& H) f
  66.     url = url.replace(/^\/{1,2}/,"");
    2 a& w- r& J7 h( Q; O( D  X
  67.     url = "http://"+url;" `; U+ l1 I9 a( M6 }
  68.   }9 p1 P0 e, E, l
  69.   // Update the submitted URL text model; r0 {% P1 @( D# C0 q! e
  70.   this.urlModel.value = url;
    6 t: G* I1 j0 v2 x( k9 L1 Q/ o
  71.   this.sceneAssistant.controller.modelChanged(this.urlModel);5 S& o/ u7 o% g2 I
  72.   var request = new Ajax.Request(url, {
    . g$ Y( k( m+ M& f, X2 |- f
  73.     method: 'get',, m: A, E4 A+ V7 |/ H$ g* \
  74.     evalJSON: 'false',
    / |. f; w6 X/ W, @' W6 x
  75.     onSuccess: this.checkOK.bind(this),8 q6 }2 W  a8 Q! E
  76.     onFailure: this.checkFailure.bind(this)1 @0 T3 E. P* X
  77.   });
    : B' ~1 ^+ ]5 q  }5 B* b$ P0 w+ ]
  78. };
    " _+ B/ |( o( g! D+ D% o# V/ K9 x* d
  79. //4 Y3 R( f* J$ K( t/ O
  80. // checkOK - called when the Ajax request is successful.
      Y# Z3 J2 r) K: d% t
  81. //3 T5 z9 D1 v  J: O
  82. AddDialogAssistant.prototype.checkOK = function(transport) {) n3 }7 {* o+ K9 U  H2 [! ?
  83.   // DEBUG - log the result0 N* ?' H4 b5 ^, U$ z
  84.   //
    4 \5 S) v- {- Q  t! W
  85.   var t = new Template("Status #{status} returned from newsfeed request.");
    " ^( v. x! o+ F: d! z! i
  86.   var success = "Feed Request Success: "+t.evaluate(transport);8 i8 V7 D6 K0 r7 u
  87.   Mojo.Log.info(success);
    8 J9 l- l( G' q9 [; {3 \5 \- V
  88.   // Work around when XML comes back in text response' F) f' z  e- }: L; F
  89.   if (transport.responseXML === null && transport.responseText !== null) {
    0 g! |! J0 J3 m3 X1 T+ q# _
  90.     Mojo.Log.info("&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;","Request not in XML format - manually converting");
    ; |% }1 N) a* J+ F8 H" h/ R( T: N
  91.     transport.responseXML = new DOMParser().parseFromString (transport.responseText, 'text/xml');
    : X1 O2 ]* l; x
  92.   }
    , r- b" G& R/ ]# c, Q& e3 c
  93.   var feedError = errorNone;+ ~; D( }9 j5 r. i, P3 f
  94.   // Push the entered feed onto feedlist and call processFeed to evaluate it.. @6 m9 U6 h; U, r7 I
  95.   // This is mainly to record the user's TITLE entry before processing
    : a0 \3 t7 {6 k3 `4 f$ o4 u
  96.   //& V# a8 \) W& z# L  l( d: F1 D! w
  97.   feedList.push({title:this.nameModel.value, url:this.urlModel.value, type:"", acFreq:0, numUnRead:0, stories:[]});
    $ D  f0 i  E/ u: N) b
  98.   feedError = ProcessFeed(transport);. R( u: n" s# l' ]. J% P
  99.   // If successful processFeed returns errorNone
    7 N% I; r" e' L; O; k# P0 ^5 A; \  c
  100.   //
    $ W/ d2 H8 g, \, c
  101.   if (feedError === errorNone) {
    7 J$ x& n. o2 Y' v5 [2 @5 ?
  102.     this.sceneAssistant.feedWgtModel.items = feedList;
    , b$ p5 k8 O  F1 }/ K' w9 m
  103.     this.sceneAssistant.controller.modelChanged(this.sceneAssistant.feedWgtModel);1 Z: ^6 ?" I5 I: ?" l, Y) G; W# d
  104.     this.widget.mojo.close();
    % D  S- }- N* p- W) }+ n! ^/ X
  105.   }
    . J$ Y( b) ?( b# f
  106.   else {
    2 _1 _* q0 G4 P: O  A$ P
  107.     feedList.pop();5 m- y7 G6 t9 r- a  W
  108.     if (feedError == errorUnsupportedFeedType) {
    ' ]: }* h8 e" D! v3 H
  109.       Mojo.Log.info("Feed ", this.urlModel.value," isn't a recognized feed type (#", errorUnsupportedFeedType, ")");
    3 o) o4 R& b# }6 v3 y
  110.       $("add-feed-title").innerHTML ="Invalid Feed - not a supported feed type";( J# U* L/ n1 M" c% u
  111.     }, {( u+ \6 g4 `5 e
  112.   }
    % v$ q# v4 H6 ^" q
  113. };
    % l; K0 Z: v1 [7 z4 W5 q  N( w
  114. AddDialogAssistant.prototype.checkFailure = function(transport) {
    ! p, n2 Q& x8 f( E
  115.   // Use the Prototype template object to generate a string from the return
    + q6 M: S+ U" ^* l% d+ S
  116.   //
    / v) E+ P3 E  k9 g& @, Q
  117.   var t = new Template($L("#{status}"));
    ) S0 z6 [) U+ O& l* X2 g6 O
  118.   var m = t.evaluate(transport);
    + N( F3 J* o' P( H4 a
  119.   // Log error and put message in status" W" \9 S; S9 d. X  K
  120.   //% G0 R  k2 l# @% `) }) x6 ~
  121.   Mojo.Log.info("Invalid feed (Status ", m, " returned).");* }4 d" ^* c! L# l3 \' P/ |) A5 Z
  122.   $("add-feed-title").innerHTML = "Invalid feed - http failure ("+m+")";
    , Y; ?& Z' g9 C+ K5 _! f2 W8 ?" P
  123. };
复制代码
3 J) J; z* y9 l; ~1 y% I' g

: B) K/ V/ E% f/ V" x7 _$ P
9 z( N5 u- x+ GThis example is from the book  Palm webOS by Mitch Allen.
回复

使用道具 举报

 楼主| 发表于 2010-4-14 13:50 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2010-4-14 13:55 | 显示全部楼层
非常感谢。
回复 支持 反对

使用道具 举报

发表于 2010-4-22 19:56 | 显示全部楼层
弄了好久终于找到showDialog显示不出的问题所在了。
1 V' U7 {  a5 M3 \0 G0 Z7 q& i- p关键在于source.js的配置,用eclips自动生成了scene和js两个文件,但是assistant只是在scene生效后才起作用,而1 O( T) Z- L0 W! C- A
showDialog只是一个dialog,并不是scene,所以虽然source.js自动帮我们生成了配置,但是还是要把scene去掉,这样就可以正常显示了。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

QQ|Archiver|手机版|小黑屋|吹友吧 ( 京ICP备05078561号 )

GMT+8, 2025-1-14 02:41 , Processed in 0.307129 second(s), 16 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表