var notesApp =
new Ext.Application({
name: "NotesApp",
useLoadMask:
true,
launch:
function() {
//
page one
NotesApp.views.notesListToolbar =
new Ext.Toolbar({
id: "notesListToolbar",
title: "Contact",
layout: "hbox",
items: [
{ xtype: "spacer" },
{
text: "New", ui: "action", handler:
function()
{
var
note = Ext.ModelMgr.create({ Id: (new Date()).getTime(),Name: "", Phone: "", Email: "" }, "Note");
//
自动加载数据
NotesApp.views.noteEditor.load(note);
NotesApp.views.viewport.setActiveItem("noteEditor", { type: "slide", direction: "left" });
}
}
]
});
Ext.regModel("Note", {
idProperty: "Id",
fields: [
{ name: "Id", type: "int" },
{ name: "Name", type: "string" },
{ name: "Phone", type: "string" },
{ name: "Email", type: "string", message: "Wrong Email Format" }
],
validations: [
{ type: "presence", field: "Id" },
{ type: "presence", field: "Name", message: "Name cannot be empty!" },
//
验证,注意type是"format"
{ type: "format", field: "Phone", matcher: /^[0-9]+$/, message: "please enter corrent number!" },
{ type: "format", field: "Email", matcher: /^[.]*$|^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/, message: "please enter corrent email!" }
]
});
NotesApp.views.NotesStore = Ext.regStore("NotesStore", {
model: "Note",
sorters: { property: "Name", direction: "ASC" },
proxy: { type: "localstorage", id: "note-store-app"
},
//
分组的时候用
getGroupString:
function
(record) {
if
(record && record.data.Name) {
return
record.get("Name")[0].toString();
}
else
{
return
"";
}
}
});
NotesApp.views.notesList =
new Ext.List({
id: "notesList",
store: "NotesStore",
//
分组
grouped:
true
,
//
右侧的字母索引
indexBar:
true,
emptyText: "<div>note is not cached</div>",
//
onItemDisclosure
itemTpl: "<div>{Name}</div>" + "<div>{Phone}</div>",
onItemDisclosure:
function(record) {
//
var
selectedNote = record;
NotesApp.views.noteEditor.load(selectedNote);
NotesApp.views.viewport.setActiveItem("noteEditor", { type: "slide", direction: "left" });
}
,
//
只要展示listview就会触发这个事件
listeners: { "render":
function
(thisComponent) {
thisComponent.getStore().load();
}
}
});
//
page two
//
home and save
NotesApp.views.noteEditorTopToolbar =
new Ext.Toolbar({
id: "noteEditorTopToolbar",
title: "Edit Contact",
layout: "hbox",
items: [
{ text: "Home", ui: "back",
handler:
function() {
NotesApp.views.viewport.setActiveItem("notesListContainer", { type: "slide", direction: "right" });
}
},
{ xtype: "spacer" },
{
text: "Save", ui: "action", handler:
function
() {
var
noteEditor = NotesApp.views.noteEditor;
//
得到record里的数据,比如:从list表单中选择一个
var
currentNote = noteEditor.getRecord();
//
将修改后的数据保存在record里,如:在文本框中修改姓名
noteEditor.updateRecord(currentNote);
//
验证,规则对应于Model里的validations
var
errors = currentNote.validate();
if
(!errors.isValid()) {
var
message = "<span style='color:red'>";
Ext.each(errors.items,
function
(rec, i) {
message += rec.message + "<br>";
});
message += "</span>";
Ext.Msg.alert("<span style='color:red'>Validate</span>", message, Ext.emptyFn);
return
;
}
//
保存notesStore中的数据
var
notesList = NotesApp.views.notesList;
var
notesStore = notesList.getStore();
if
(notesStore.findRecord('Id', currentNote.data.Id) ===
null
) {
notesStore.add(currentNote);
}
notesStore.sync();
notesStore.sort([{ property: 'Name', direction: 'ASC'}]);
//
刷新notesList中的数据
notesList.refresh();
NotesApp.views.viewport.setActiveItem('notesListContainer', { type: 'slide', direction: 'right' });
}
}
]
});
//
delete
NotesApp.views.noteEditorBottomToolbar =
new Ext.Toolbar({
id: "noteEditorBottomToolbar",
dock: "bottom",
layout: "hbox",
items: [
{ xtype: "
spacer" },
{ iconCls: "trash", iconMask:
true
, handler:
function
() {
//
得到record里的数据
var
currentNote = NotesApp.views.noteEditor.getRecord();
//
删除notesStore中的数据
var
notesList = NotesApp.views.notesList;
var
notesStore = notesList.getStore();
if
(notesStore.findRecord('Id', currentNote.data.Id)) {
notesStore.remove(currentNote);
}
notesStore.sync();
//
刷新notesList中的数据
notesList.refresh();
NotesApp.views.viewport.setActiveItem('notesListContainer', { type: 'slide', direction: 'right' });
}
}
]
});
//
editor content
NotesApp.views.noteEditor =
new Ext.form.FormPanel({
id: "noteEditor",
items: [
{ xtype: "textfield", name: "Name", label: "Name", required:
true },
{ xtype: "textfield", name: "Phone", label: "Phone" },
{ xtype: "textfield", name: "Email", label: "Email" }
],
dockedItems: [NotesApp.views.noteEditorTopToolbar, NotesApp.views.noteEditorBottomToolbar]
});
NotesApp.views.notesListContainer =
new Ext.Panel({
id: "notesListContainer",
layout: "fit",
dockedItems: [NotesApp.views.notesListToolbar],
items: [NotesApp.views.notesList]
});
NotesApp.views.viewport =
new Ext.Panel({
fullscreen:
true,
//
fullscreen
layout: "card",
cardSwitchAnimation: "slide",
items: [NotesApp.views.notesListContainer
, NotesApp.views.noteEditor
]
});
}
});