People里對name和age的改變做出了對應的事件
一個事件要符合三個步驟:定義、發布、訂閱
1 Ext.namespace(Ext.dojochina);
2
3 Ext.dojochina.People = function() {
4 this.addEvents( /**事件的定義*/
5 "namechange",
6 "agechange"
7 );
8 };
9
10
11 Ext.extend(Ext.dojochina.People, Ext.util.Observable, {
12 name:"",
13 age:"",
14
15 setName:function(_name) {
16 if(this.name != _name) {
17 this.fireEvent("namechange", this, this.name, _name); /**事件的發布*/
18 this.name = _name;
19 }
20 },
21
22 setAge: function(_age) {
23 if(this.age != _age) {
24 this.fireEvent("agechange", this, this.age, _age);
25 this.age = _age;
26 }
27 }
28 });
JSP頁面上(實現了事件的訂閱)
1 <script type="text/javascript" src="./scripts/ext/demo/People.js"></script>
2
3 <script type="text/javascript">
4 var _people = null;
5
6 //按鈕點擊觸發的事件
7 button_click = function(){
8 _people.setName(prompt("請輸入你的名字!", ""));
9 _people.setAge(prompt("請輸入你的年齡!",""));
10 }
11
12 Ext.onReady(function(){
13 var txt_name = Ext.get("txt_name");
14 var txt_age = Ext.get("txt_age");
15
16 _people = new Ext.dojochina.People();
17 /**事件的訂閱*/
18 _people.on("namechange", function(_people, _old, _new){
19 txt_name.dom.value = _new;
20 });
21
22 _people.on("agechange", function(_people, _old, _new){
23 txt_age.dom.value = _new;
24 });
25
26 /**事件的隊列,同一個事件多次訂閱*/
27 _people.on("namechange", function(_people, _old, _new){
28 document.title = _new;
29 });
30 });
31
32 </script>
33
34 </head>
35
36 <body>
37 <h4><font color="blue">這是對事件調用的測試頁面</font></h4>
38 姓名:<input type="text" id="txt_name"> <br />
39 年齡:<input type="text" id="txt_age">
40 <input type="button" value="輸入" onclick="button_click()">
41
42 </body>
但是這個例子我一直沒找到錯誤,使用谷歌瀏覽器的時候只能出現第一個輸入框就什么效果也沒有了,要是哪位大蝦發現錯誤請及時告訴我啊~~謝謝嘍

~~自己剛發現了錯誤,我的代碼是用MyEclipse生成的,JS文件是使用File新建的,命名的時候忘了寫后綴了~~

我可是活生生找了一下午啊~~可是記清楚了~~~!!
事件Event"
trackback:ping="http://www.tkk7.com/wufang5/services/trackbacks/239941.aspx" />
-->