Welcome

首页 / 脚本样式 / ExtJS / ExtJs中使用自定义事件

ExtJs中使用自定义事件2010-12-05 博客园 TerryLiang以下代码定义了一个用于更新文章或添加文章的窗体:

1Cms.ModifyArticleWindow = Ext.extend(Ext.Window, {
2 title: "添加文章",
3 modal: true,
4 layout: "fit",
5 width: 650,
6 height: 600,
7 md: "add",
8 bufferResize: false,
9
10 initComponent: function() {
11 var url = "/admin/addarticle";
12 var categoryId = this.categoryId;
13 var articleId;
14 var title;
15 var author;
16 var isNewsPhotos = false;
17 var photoUrl;
18 var summary;
19 var content;
20
21 if (this.md && this.md == "update") {
22 this.title = "修改文章";
23 url = "/admin/updatearticle";
24 articleId = this.article.Id;
25 title = this.article.Title;
26 author = this.article.Author;
27 isNewsPhotos = this.article.IsNewsPhotos;
28 photoUrl = this.article.PhotoUrl;
29 summary = this.article.Summary;
30 content = this.article.Content;
31 }
32
33 var win = this;
34 var update = this.onUpdate;
35
36 this.items = [{
37 xtype: "form",
38 url: url,
39 width: 640,
40 height: 400,
41 labelWidth: 50,
42 monitorValid: true,
43 frame: true,
44 items: [{
45 xtype: "hidden",
46 name: "categoryId",
47 value: categoryId
48 }, {
49 xtype: "hidden",
50 name: "Id",
51 value: articleId
52 }, {
53 xtype: "textfield",
54 fieldLabel: "标题",
55 name: "title",
56 anchor: "100%",
57 allowBlank: false,
58 value: title
59 }, {
60 xtype: "textfield",
61 fieldLabel: "作者",
62 name: "author",
63 anchor: "100%",
64 value: author
65 }, {
66 layout: "column",
67 items: [{
68 columnWidth: 0.3,
69 layout: "form",
70 items: {
71 xtype: "checkbox",
72 fieldLabel: "是否是图片新闻",
73 name: "isNewsPhotos",
74 inputValue: true,
75 checked: isNewsPhotos
76 }
77 }, {
78 columnWidth: 0.7,
79 layout: "form",
80 items: {
81 xtype: "textfield",
82 fieldLabel: "图片路径",
83 name: "photoUrl",
84 anchor: "90%",
85 value: photoUrl
86 }
87}]
88 }, {
89 xtype: "htmleditor",
90 fieldLabel: "摘要",
91 name: "summary",
92 height: 100,
93 anchor: "98%",
94 value: summary
95 }, {
96 xtype: "htmleditor",
97 fieldLabel: "内容",
98 name: "content",
99 height: 270,
100 anchor: "98%",
101 allowBlank: false,
102 value: content
103}],
104 buttons: [{
105 text: "确定",
106 formBind: true,
107 handler: update,
108 scope: win
109 }, {
110 text: "取消",
111 handler: function() {
112 win.close();
113 }
114}]
115
116}];
117 Cms.ModifyArticleWindow.superclass.initComponent.call(this);
118 //定义了保存后执行的事 件
119 this.addEvents("afterSave");
120 },
121 onUpdate: function() {
122 var win = this;
123 var form = this.items.items[0].form;
124
125 form.submit({
126 method: "POST",
127 waitTitle: "",
128 waitMsg: "正在处理",
129 success: function(form, action) {
130 //返回结果后触发保存事件
131 win.fireEvent ("afterSave", win, win.md, true);
132 },
133 failure: function(form, action) {
134 // 返回结果后触发保存事件
135 win.fireEvent("afterSave", win, win.md, true);
136
137 if (action.failureType == "server") {
138 obj = Ext.util.JSON.decode (action.response.responseText);
139 Ext.Msg.alert("更新失败! ", obj.errors.reason);
140 } else {
141 Ext.Msg.alert("警告 !", "服务不可用 : " + action.response.responseText);
142 }
143
144 form.reset();
145 }
146 });
147 }
148 });