Chinaunix首页 | 论坛 | 博客
  • 博客访问: 925393
  • 博文数量: 264
  • 博客积分: 10107
  • 博客等级: 上将
  • 技术积分: 2455
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-09 16:34
文章分类

全部博文(264)

文章存档

2012年(1)

2011年(11)

2010年(128)

2009年(82)

2008年(42)

我的朋友

分类: 系统运维

2010-03-25 16:14:30

Flex与JavaScript交互的例子(二)
2008-09-25 21:44

对应的initApp()方法:

public function initApp():void
{
   if (ExternalInterface.available)
     ExternalInterface.addCallback("addPerson", addPerson);
}

由于要吧资料添加的datagrid中世界上就是增加到dataprovider中的,其本身是一个arrayCollection,那就可以使用additem的方法来增加信息:

public function addPerson(name:String, age:String,
   sex:String):void
{
  (dgPeople.dataProvider as ArrayCollection).addItem(
      {Name: name, Age: age, Sex: sex});
}

为了能够访问到flex资源我们调用getFlexApp('FlexJSTutorial')。这里'FlexJSTutorial'是嵌入在 web中的flash所在的语句块的【注意配对】。对应的在web中的addPerson方法定义如下:

function addPerson()
{
  var name = document.getElementById('txtName').value;
  var age = document.getElementById('txtAge').value;
  var sex = document.getElementById('selSex').value;
   getFlexApp('FlexJSTutorial').addPerson(name, age, sex);
}

做的事情是收集用户输入的资料,调用flex中的addperson()方法。最后就是刚才提到的getFlexAPP()方法--实际上就是一个 返回页面的flex app方法。这个方法跟浏览器类型有关,还需要注意的是页面中object标记中含有id和name两个属性。这两个属性对于取得flex app很重要。

function getFlexApp(appName) {
   if (navigator.appName.indexOf ("Microsoft") !=-1) {
     return window[appName];
  } else {
     return document[appName];
}

最后一步就是提供用户输入的界面:

<table style="border-spacing:5px;">
  <tr>
    <td style="border-style:none;padding:0px;">Name:</td>
    <td style="border-style:none;padding:0px;">
      <input id="txtName" type="text" />
    </td>
  </tr>
  <tr><td style="border-style:none;padding:0px;">Age:</td>
    <td style="border-style:none;padding:0px;">
      <input id="txtAge" type="text" />
    </td>
  </tr>
  <tr><td style="border-style:none;padding:0px;">Sex:</td>
    <td style="border-style:none;padding:0px;">
      <select id="selSex" style="width:100px;">
        <option value="Male">Male</option>
        <option value="Female">Female</option>
      </select>
    </td>
  </tr>
  <tr>
    <td colspan="2" style="border-style:none;padding:0px;">
      <input type="button" id="butAddPerson"
        onclick="addPerson()" value="Add Person" />
    </td>
  </tr>
</table>

附上:flex程序:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=""
   layout="absolute" width="482" height="348"
   initialize="initApp()"
   viewSourceURL="../files/JSTutorial.mxml">
<mx:Script>
  <![CDATA[
     import mx.collections.ArrayCollection;
     import flash.external.*;
   
     public function initDG():void
    {
       var people:Array = new Array();
       people.push({Name: "Charlie", Age: "23", Sex: "Male"});
       people.push({Name: "Brandon", Age: "23", Sex: "Male"});
       people.push({Name: "Mike", Age: "23", Sex: "Male"});
       people.push({Name: "Caroline", Age: "23", Sex: "Female"});
       var peopleCollection:ArrayCollection =
         new ArrayCollection(people);
       dgPeople.dataProvider = peopleCollection;
       dgPeople.selectedIndex = 0;
    }
   
     public function addPerson(name:String, age:String,
       sex:String):void
    {
      (dgPeople.dataProvider as ArrayCollection).addItem(
          {Name: name, Age: age, Sex: sex});
    }
   
     public function initApp():void
    {
       if (ExternalInterface.available)
         ExternalInterface.addCallback("addPerson", addPerson);
    }
   
     public function jsDisplayPerson():void
    {
       if (ExternalInterface.available) {
         ExternalInterface.call("displayPerson",
           dgPeople.selectedItem);
         lblMessage.text = "Data Sent!";
      } else
         lblMessage.text = "Error sending data!";
    }
  ]]>
</mx:Script>
<mx:Panel id="pnlMain" x="10" y="10" width="462" height="328"
   layout="absolute" title="Simple Javascript Interaction">
<mx:DataGrid id="dgPeople" x="10" y="10" initialize="initDG()"
   width="422" height="229">
    <mx:columns>
      <mx:DataGridColumn headerText="Name" dataField="Name"/>
      <mx:DataGridColumn headerText="Age" dataField="Age"/>
      <mx:DataGridColumn headerText="Sex" dataField="Sex"/>
    </mx:columns>
  </mx:DataGrid>
  <mx:Button x="10" y="256" label="JavaScript Display"
     id="butJSDisplay" click="jsDisplayPerson()"/>
  <mx:Label x="149" y="260" id="lblMessage"/>
</mx:Panel>
</mx:Application>

页面测试以及代码:http://blog.paranoidferret.com/files/flexjavascript.html

阅读(938) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~