TWaver - 專注UI技術

          http://twaver.servasoft.com/
          posts - 171, comments - 191, trackbacks - 0, articles - 2
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          有好多同學問當瀏覽器關閉時, Flex如何能得到通知, 提醒用戶保存數據. 這是個很普遍的問題, 下面就演示一下如何做.

          Flex無法知道瀏覽器何時關閉, 但javascript可以(window.onbeforeunload), 所以思路就是:
          1. Flex提供方法, 能讓javascript調用, 以便在瀏覽器關閉時, 判斷是否有數據需要保存, 如果有, 就提示, 沒有就啥都不干<?xml version="1.0" encoding="utf-8"?>

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal"
                          xmlns:tw
          ="http://www.servasoftware.com/2009/twaver/flex"
                          creationComplete
          ="init();" paddingBottom="0" paddingLeft="0" paddingRight="0" paddingTop="0">
              
          <mx:Script>
                  
          <![CDATA[
                      
          import mx.controls.Alert;

                      
          import twaver.*;
                      
          import twaver.network.interaction.InteractionEvent;

                      
          private var box:ElementBox = new ElementBox();

                      
          private function init():void {
                          
          this.initBox();
                          
          // Add callback, javascript use it to check whether popup message when browser is closing
                          ExternalInterface.addCallback("needSave", needSave);
                          
          // Enable save location button once location changed
                          network.addInteractionListener(function(e:InteractionEvent):void {
                              
          if(InteractionEvent.LAZY_MOVE_END == e.kind
                                      
          || InteractionEvent.LIVE_MOVE_END == e.kind) {
                                  
          if(!btnSave.enabled){
                                      btnSave.enabled 
          = true;
                                  }
                              }
                          });
                      }

                      
          private function initBox():void {
                          var from:Node 
          = new Node();
                          from.name 
          = "From";
                          from.location 
          = new Point(100200);
                          box.add(from);

                          var to:Node 
          = new Node();
                          to.name 
          = "To";
                          to.location 
          = new Point(400500);
                          box.add(to);

                          box.add(
          new Link(from, to));
                          
          this.network.elementBox = box;
                      }

                      
          private function save():void {
                          saveData();
                          btnSave.enabled 
          = false;
                      }

                      
          private function saveData():void {
                          Alert.show(
          "Data saved");
                      }

                      
          public function needSave():Boolean {
                          
          return btnSave.enabled;
                      }
                  ]]
          >
              
          </mx:Script>
              
          <mx:VBox width="100%" height="100%">
                  
          <mx:HBox width="100%">
                      
          <mx:Button id="btnSave" label="Save Location" click="save()" enabled="false"/>
                  
          </mx:HBox>
                  
          <tw:Network id="network" width="100%" height="100%"/>
              
          </mx:VBox>
          </mx:Application>

           

           2. html頁面添加window.onbeforeunload事件, 判斷如果Flex有內容要保存, 就彈出提示:
          <script>
              window.onbeforeunload = function (evt) {
                  var demo = document.demo || window.demo;
                  if (demo.needSave()) {
                      var message = 'You did not save your data. Do you really want to quit?';
                      if (!evt) {
                          evt = window.event;
                      }
                      if (evt) {
                          evt.returnValue = message;
                      }
                      return message;
                  }
              }
          </script>
          完整代碼見附件:TestWindowClose

          評論

          # re: 瀏覽器關閉時, 如何提醒用戶保存Flex上的拓撲數據  回復  更多評論   

          2012-03-01 09:17 by 彈弓
          樓主不是用的360瀏覽器吧

          # re: 瀏覽器關閉時, 如何提醒用戶保存Flex上的拓撲數據  回復  更多評論   

          2012-03-01 09:51 by 我是看評論的
          樓上的, 我笑了, 明明是safari, 怎么成360了

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 长岭县| 即墨市| 和政县| 柘荣县| 缙云县| 五常市| 南江县| 静海县| 防城港市| 承德市| 比如县| 平罗县| 怀安县| 阿拉善盟| 武鸣县| 铜梁县| 临夏县| 泽普县| 呼图壁县| 华阴市| 望谟县| 靖西县| 田林县| 洪泽县| 鄂托克前旗| 牙克石市| 扶风县| 盐亭县| 克东县| 富民县| 黎城县| 唐河县| 迁西县| 新邵县| 布尔津县| 海林市| 正安县| 伊宁市| 新闻| 鄂伦春自治旗| 阜平县|