找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 2123|回复: 3

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

[复制链接]
发表于 2010-4-14 13:48 | 显示全部楼层 |阅读模式
ShowDialog() (Custom Dialog) 7 J; r  ^' w4 _$ }7 ?9 H
Example usage:
  1. // addNewFeed - triggered by "Add..." item in feed list and invokes the AddDialog0 E$ B+ E1 d* j6 L
  2. // Assistant defined above./ f9 B0 [- z1 X% \% Q8 n! e
  3. //2 |- \! h6 }4 h8 e  j
  4. FeedListAssistant.prototype.addNewFeed = function() {
    , ^# O6 {# W- z6 L8 x
  5.   this.controller.showDialog({
    7 j1 R" u" c% k- d( {
  6.     template: 'feedList/addFeed-dialog',' a/ G  e+ E( t% `% @  E& s
  7.     assistant: new AddDialogAssistant(this); k4 q9 L# ]3 P( M
  8.   });  H; @- B* {# L- t) l& c! X- K
  9. };
复制代码

" a9 A4 b( e& c3 [
0 }) A+ J* ^5 T- i3 |A 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.% c1 t. D4 G- x( a" [- j( @5 r# R' I

) x! ^) R" h; Y5 aTemplate file:
  1. <div>
    : h4 n9 d3 T' k  E3 Q; H* \$ `' v
  2.   <span id="add-feed-title" class="palm-dialog-title">Add News Feed Source</span>% h; `: ]. ~  ?; \, M% _
  3.   <div class="palm-group unlabeled">* M- v3 h+ D& `0 ]# ]4 j% K: K/ x
  4.     <div class="palm-list">% S# l/ R* J$ q$ y; k
  5.       <div class='palm-row first'>
    1 _' B' N0 U# i* p6 Y
  6.         <div class="palm-row-wrapper textfield-group" x-mojo-focus-highlight="true">$ h+ H+ O) m2 Y7 \. a- }
  7.           <div class="title">
    ( N: V! g* T+ b6 v
  8.             <div class="label">URL</div>
    2 y; p( v& k$ B/ M( r  u. p+ J9 F- s
  9.             <div id="newFeedURL" x-mojo-element="TextField" align="left"></div>
    # `" Y) Z1 o; s# O: P7 D: _* C
  10.           </div>
    & O2 e2 ?7 V" ]7 T! o
  11.         </div>
    4 T7 D) ~/ d9 c$ O! G) a+ P( F
  12.       </div>" M  I8 q7 K1 g8 T
  13.       <div class='palm-row last'>
    - Q. y  y. z7 |% D; _; g" z
  14.         <div class="palm-row-wrapper textfield-group" x-mojo-focus-highlight="true">1 ]$ v+ r  C- n
  15.           <div class="title">
    ( t( p& [% `" J; ?! e" }
  16.             <div class="label">Title</div>- e. }6 v1 U9 V" }0 F6 B3 `2 t
  17.             <div id="newFeedName" x-mojo-element="TextField" align="left"></div>3 v7 o3 F+ g6 Y3 ]$ G4 e$ H& D; ]
  18.           </div>: G) g, B8 t( `' }
  19.         </div>' ]3 x3 |" [  A0 l# M
  20.       </div>
    ( X4 c  Q: ^6 s9 r" _7 Z1 f3 {  W0 G
  21.     </div>8 S% |+ L1 _. O: a# M
  22.   </div>% A& {( M, V9 b5 L
  23.   <div x-mojo-element="Button" id="okButton"></div>+ o* b* H1 T+ Y( |" T
  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.4 e! j+ u. c1 d5 R- n8 y$ Z

% {. ~! p) ?; e% {3 O$ |# @Assistant file:
  1. // AddDialogAssistant - simple controller for adding new feeds to the list.
    1 O+ @/ M9 Q+ Q& f9 K9 E5 b1 v
  2. // Invoked by the FeedListAssistant when the "Add..." list item is selected' f* Z8 V8 E' b* S" f6 g) }: U- P
  3. // on the feed list.2 y0 b3 n( l$ y* L; m
  4. //' l( {( _  v: \) Z, X# G$ W7 O) H
  5. // The dialog displays two text fields (URL and Name) and an OK button.
    . l, c0 e4 c% r& c7 C. o& D) ~
  6. // Either the user enters a feed URL and a name, followed by OK or a/ H$ q& l: [2 s/ B
  7. // back swipe to close. If OK, the feed header is checked through an Ajax
    : R; O6 r6 n, r2 L1 b
  8. // request and if valid, the feed updated and dialog closed. If an error,
    " c" Q! ~: }% G4 q) C
  9. // the posted in place of title and dialog remains open.
    2 i; w) |! }. E% M7 _1 b4 ?
  10. // Swipe back cancels and returns back to the FeedListAssistant.
    9 e) a$ [) P" v) t* X# Z3 z0 G
  11. //
    1 U1 R6 |# S! G8 s- d8 B7 o
  12. function AddDialogAssistant(sceneAssistant) {0 }& [0 ~# R2 l
  13.   this.sceneAssistant = sceneAssistant;% l# ]! V: [9 X. O0 d
  14. }
    + _" [+ n7 c- ?
  15. AddDialogAssistant.prototype.setup = function(widget) {
    2 s: D# m# ]% D, i* N
  16.   this.widget = widget;* _' V  t8 r8 Z9 @. u
  17.   // Setup text field for the new feed's URL5 f; O1 Q- M8 G8 ^
  18.   //# T+ p* Q2 M. k2 L
  19.   this.sceneAssistant.controller.setupWidget("newFeedURL",
    / R0 q  n& E  Z2 U4 @) H, d
  20.     this.urlAttributes = {9 X: D8 E# [& g$ G  C
  21.       property: "value",6 v* }, M) @# f; @( t4 L
  22.       hintText: "RSS or ATOM feed",
    8 e7 x. z9 n+ A/ Y
  23.       focus: true,8 Q; U8 H# M1 z. G
  24.       limitResize: true,4 `1 n- ?3 E' H4 `, G  \
  25.       textReplacement: false,- w2 A6 c/ Y9 F! d4 g
  26.       enterSubmits: false: @- Z* c+ b1 M4 A6 d/ d
  27.     },4 U8 `5 f! f8 y" @, Z
  28.     this.urlModel = {value : ""}
    ( M3 G; o! F# x% l/ j
  29.   );0 K4 U. K: Z7 [
  30.   // Setup text field for the new feed's name
    ' J; j, r3 V6 b8 I4 j: h2 P$ H) Z
  31.   //" n) [2 [1 c6 E( D* Z
  32.   this.sceneAssistant.controller.setupWidget("newFeedName",
    ; V/ g: k1 o" h0 C1 H" W
  33.     this.nameAttributes = {2 L& p* d% {2 W# Y, T. q- p$ H# W$ f
  34.       property: "value",2 E4 J2 `8 p6 H- e0 x. U
  35.       hintText: "Optional",0 R7 W. i! q# J* X7 X1 ]% z3 b
  36.       limitResize: true,: R" I" L  L4 d+ ~
  37.       textReplacement: false,
    ! J% T1 |* o/ a( ]0 c) C. }$ ]
  38.       enterSubmits: false
    8 U& T" C6 k0 H) I
  39.     },1 _7 x& x8 b# g
  40.     this.nameModel = {value : ""
    3 |+ e( r9 j( n9 L$ O9 S
  41.   });6 Z' H$ Q( B3 X. ~# p5 ]3 ^
  42.   // Setup button and event handler
    " t7 J7 x' t/ h# u$ P, v
  43.   //
    : s) x* Q/ B) s- [
  44.   this.sceneAssistant.controller.setupWidget("okButton",  ?8 _" T- F* L, F! h9 L; W
  45.     this.attributes = {},
    3 U. b6 [, S! I  Z3 ~
  46.     this.model = {2 D6 L9 @! }) m: G# L
  47.       buttonLabel: "Add Feed",
    * Y+ d. I7 j$ u; H4 w/ }
  48.       buttonClass: "addFeedButton",4 D. w# X8 I: l5 T/ n* d; \; X( F
  49.       disabled: false
    9 U$ e' _. E4 u
  50.     }" T, ^, q$ n' V2 F1 B4 m. ?. D
  51.   );
      Y' U- G* |+ B: y$ H9 a5 _8 E
  52.   Mojo.Event.listen($('okButton'), Mojo.Event.tap, this.checkIt.bindAsEventListener(this));" E* G( R( H& {5 F
  53. };. W" S1 Z+ D6 u$ Z3 J. s, X: @! D
  54. // ------------------------------------------------------------------------
    6 l" `8 X: V" X* W& O6 ?
  55. // Add Feed Functions" ]8 K/ u9 e. G4 j; F5 M
  56. //1 B; [2 ~9 k$ l' Q2 x5 ?
  57. // checkIt - called when Add feed OK button is clicked' U* V, M: K* v. ^7 S5 t5 ~: H
  58. //
    9 Q, e: v: H6 t1 q$ |
  59. AddDialogAssistant.prototype.checkIt = function() {
    + @; U3 Z% I3 r& k
  60.   // Check for 'http://' on front or other legal prefix$ j9 d/ k$ y5 U0 k% H, E6 ^  {8 K
  61.   // assume any string of 1 to 5 alpha characters followed
    ; I( y: x. {( C% P6 H% Q
  62.   // by ':' is legal, otherwise prepend "http://"+ M/ l  u. z: q+ Z' j/ B
  63.   //
    7 d( p8 _: G5 \8 H" p0 J
  64.   var url = this.urlModel.value;
    6 P5 X/ X' ?- ?
  65.   if (/^[a-z]{1,5}:/.test(url) === false) {9 L5 f2 T, ^3 w& j- j
  66.     url = url.replace(/^\/{1,2}/,"");0 A, u4 B: D3 z
  67.     url = "http://"+url;
    ! A. H$ R5 I, u
  68.   }
    ; \/ U2 S) Z+ Z+ J" P0 k, ?9 D
  69.   // Update the submitted URL text model
    ' k) _, p6 Z- N* j( Q. e
  70.   this.urlModel.value = url;1 t' r9 U' d; j& R7 F: K# \# m
  71.   this.sceneAssistant.controller.modelChanged(this.urlModel);
    & E; x  o! B: m2 L* \* W2 {( ?" j
  72.   var request = new Ajax.Request(url, {% Q3 q4 H) X& N, o. R0 h
  73.     method: 'get',
    . `- y/ W, q& \! S% ?
  74.     evalJSON: 'false',
    0 j1 q9 D7 _3 j% @) M3 G) k' ]
  75.     onSuccess: this.checkOK.bind(this),. u* g7 C* i) `  w% z# e
  76.     onFailure: this.checkFailure.bind(this)
    0 ~) Q4 `$ L9 ^- I5 E* V- }4 P
  77.   });
    5 W2 Q) P: T; o8 k" X* H
  78. };" K5 o1 @/ d% ~4 B4 {( A: k6 W
  79. //, R' w' Z6 K4 S& P  [. L
  80. // checkOK - called when the Ajax request is successful.% p% z' c& {1 j6 g& W1 B
  81. //
    & ^( ]3 f1 }/ P* K6 S" o! Y2 k+ x
  82. AddDialogAssistant.prototype.checkOK = function(transport) {
    / Y; w5 Z- x# y
  83.   // DEBUG - log the result
    4 z8 s  U% s& G( A* M
  84.   //$ v1 f$ K7 r' m/ c3 y4 i8 Y6 ]" L
  85.   var t = new Template("Status #{status} returned from newsfeed request.");* J- t& ?7 `' d* ?* Z' i; ^
  86.   var success = "Feed Request Success: "+t.evaluate(transport);
    1 B  @4 p6 X; l: b$ O5 C# z
  87.   Mojo.Log.info(success);/ @# X  t9 U' A" I1 Z9 j' }5 e
  88.   // Work around when XML comes back in text response
    % ]0 c, ^- E, @& e1 O% G
  89.   if (transport.responseXML === null && transport.responseText !== null) {$ J7 }/ P* b, N/ Z! |
  90.     Mojo.Log.info("&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;","Request not in XML format - manually converting");1 `" G, d( X7 R
  91.     transport.responseXML = new DOMParser().parseFromString (transport.responseText, 'text/xml');
    . Y- F. X& E+ |- K* i* ?: w6 g
  92.   }& |' g- }7 F  A0 C# k4 Y# T
  93.   var feedError = errorNone;7 l4 \$ G4 G" w# T$ Z
  94.   // Push the entered feed onto feedlist and call processFeed to evaluate it.( k/ D4 M" X+ S2 D2 C; H
  95.   // This is mainly to record the user's TITLE entry before processing
    8 V* B  v, R2 t+ J
  96.   //4 r' {7 G: H3 i/ G, i% g; J
  97.   feedList.push({title:this.nameModel.value, url:this.urlModel.value, type:"", acFreq:0, numUnRead:0, stories:[]});
    7 n+ S; _! j5 {1 i0 {* I
  98.   feedError = ProcessFeed(transport);
    $ L/ {, }3 I! R3 h: J# l. E
  99.   // If successful processFeed returns errorNone
    ( l" [2 a8 }  x  h6 ~- D/ j0 g" H
  100.   //
    " a/ {8 F) l9 ^/ L+ u
  101.   if (feedError === errorNone) {9 D  l0 Q. C# v/ ^) I
  102.     this.sceneAssistant.feedWgtModel.items = feedList;  W; ~* d- X" A: C9 |, d. r
  103.     this.sceneAssistant.controller.modelChanged(this.sceneAssistant.feedWgtModel);4 B5 Z' s* o0 o! f) Q' h, \
  104.     this.widget.mojo.close();6 I& q  ~& u' e0 p8 N+ K1 P
  105.   }( Q& J6 b8 u) C* D" z; J# I
  106.   else {
    3 `6 w% K$ l! X6 ^
  107.     feedList.pop();( w+ Q1 r$ J/ l0 }/ w
  108.     if (feedError == errorUnsupportedFeedType) {
    " g2 K7 s! {; t) E) K$ t( v( ?5 P
  109.       Mojo.Log.info("Feed ", this.urlModel.value," isn't a recognized feed type (#", errorUnsupportedFeedType, ")");
    " P- ^8 U# D1 Z$ C5 @, I
  110.       $("add-feed-title").innerHTML ="Invalid Feed - not a supported feed type";
    ) i/ _9 a5 Z1 C6 s
  111.     }
    1 M$ Z# a# B2 m) n& M3 F) q; ]
  112.   }$ x# X8 F) u- {1 Y" D4 H
  113. };0 \: |4 Y( a5 ]; S4 T+ E
  114. AddDialogAssistant.prototype.checkFailure = function(transport) {
    $ w1 Z. ]9 {' G) l/ a) P4 z0 c
  115.   // Use the Prototype template object to generate a string from the return
    6 M7 `  [4 g7 v  L1 a) @
  116.   //0 W, W" r' I  ?# y8 M9 H
  117.   var t = new Template($L("#{status}"));* D( x6 b* S. a* b
  118.   var m = t.evaluate(transport);5 v: F2 `( W6 }0 }, z$ O
  119.   // Log error and put message in status0 X3 S8 D; h" g- T$ a6 e0 W
  120.   //
    0 \4 @( }5 |* O+ @& ?
  121.   Mojo.Log.info("Invalid feed (Status ", m, " returned).");
    , w# u3 l9 N; O( L  X$ P4 R. N
  122.   $("add-feed-title").innerHTML = "Invalid feed - http failure ("+m+")";' p6 }8 P& f- L
  123. };
复制代码
) ^4 ?7 q  d! X9 Z3 q) N

0 q& L9 k3 Q2 L5 @9 P
2 j/ G! |7 b; H1 V6 o1 `( Y" F" pThis 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显示不出的问题所在了。3 j  L& G6 d: O) c8 o$ x
关键在于source.js的配置,用eclips自动生成了scene和js两个文件,但是assistant只是在scene生效后才起作用,而) m4 _5 @- w7 e" B
showDialog只是一个dialog,并不是scene,所以虽然source.js自动帮我们生成了配置,但是还是要把scene去掉,这样就可以正常显示了。
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-14 02:12 , Processed in 0.300968 second(s), 15 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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