/*公共类*/
Ext.define('myUtil', {
statics: {
//store公用加载方法
storeLoadById: function (id) {
var store = Ext.getStore(id);
this.storeLoad(store);
},
//store加载方法,带参数
storeLoadByParams: function (store, params) {
store.removeAll();
store.setProxy({
extraParams: params
});
this.storeLoad(store, true);
},
//加载stroe
storeLoad: function (store, isUp) {
var me = this;
if (store.getCount() < 1 || isUp) {
store.loadPage(1);
}
},
//list->info公用加载方法
showInfo: function (record, view, url, params, Summary) {
var me = this;
if (record.data.Summary) {
view.setData(record.data);
return;
}
Ext.Ajax.request({
url: url,
params: params,
success: function (result, request) {
result = Ext.decode(result.responseText);
record.set(result);
view.setData(record.data);
}
});
},
/*为Ext.Viewport添加一个消息提示组件*/
addMessage: function () {
Ext.Viewport.setMasked({
xtype: 'loadmask',
cls: 'message',
transparent: true,
indicator: false
});
this.hideMessage();
},
/*显示一个消息提示*/
showMessage: function (mes, autoHide) {
var message = this.getMessage();
message.setMessage(mes);
message.isHidden() && message.show();
//是否自动关闭提示
if (autoHide) {
setTimeout(function () {
!message.isHidden() && message.hide();
},
500);
}
},
/*隐藏消息提示*/
hideMessage: function () {
var message = this.getMessage(); !message.isHidden() && message.hide();
},
//活动消息组件
getMessage: function () {
return Ext.Viewport.getMasked();
},
//重写Lsit相关
overrideList: function () {
//重写分页插件
Ext.define("Ext.zh.plugin.ListPaging", {
override: "Ext.plugin.ListPaging",
config: {
//自动加载
autoPaging: true,
//滚动到最底部时是否自动加载下一页数据
noMoreRecordsText: '没有更多内容了',
loadMoreText: '加载更多...' //加载更多按钮显示内容
}
});
//重写List
Ext.define("Ext.zh.List", {
override: "Ext.List",
config: {
//取消选择效果
selectedCls: '',
//禁用加载遮罩,防止跳转时页面卡顿,使用统一的遮罩效果
loadingText: false,
emptyText: '没有更多内容了'
}
});
},
//重写Pick相关
overridePick: function () {
Ext.apply(Ext.DateExtras.monthNames, ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]);
Ext.define("Ext.zh.DatePicker", {
override: "Ext.picker.Date",
config: {
yearFrom: 2000,
monthText: '月',
dayText: '日',
yearText: '年'
}
});
Ext.define("Ext.local_zh_cn.Picker", {
override: "Ext.picker.Picker",
applyDoneButton: function (config) {
if (config) {
if (Ext.isBoolean(config)) {
config = {};
}
if (typeof config == "string") {
config = {
text: config
};
}
Ext.applyIf(config, {
ui: 'action',
align: 'right',
text: '确定'
});
}
return Ext.factory(config, 'Ext.Button', this.getDoneButton());
},
applyCancelButton: function (config) {
if (config) {
if (Ext.isBoolean(config)) {
config = {};
}
if (typeof config == "string") {
config = {
text: config
};
}
Ext.applyIf(config, {
align: 'left',
text: '取消'
});
}
return Ext.factory(config, 'Ext.Button', this.getCancelButton());
}
});
},
//重写ajax
overrideAjax: function () {
var me = this;
//开始加载
Ext.Ajax.on('beforerequest',
function (connection, options) {
if (!options.hidMessage) {
me.showMessage('正在努力加载中...');
}
});
//加载成功
Ext.Ajax.on('requestcomplete',
function (connection, options) {
me.hideMessage();
});
//加载失败
Ext.Ajax.on('requestexception',
function (connection, options) {
if (!options.hidMessage) {
me.showMessage('加载失败,请稍后再试...', true);
}
});
},
//将数组中时间格式化
dateFormat: function (values, naem) {
if (values[naem]) {
values[naem] = Ext.Date.format(values[naem], 'Y-m-d');
} else {
//没有值则移除
delete values[naem];
}
},
//显示pick
showPick: function (xtype, params) {
var pick = Ext.create(xtype);
Ext.Viewport.add(pick);
pick.show(params);
},
//结束pick
endPick: function (xtype) {
var pick = Ext.Viewport.down(xtype);
if (pick) {
pick.endPick();
}
},
//Viewport添加一个视图
ePop: function (xtype) {
var me = Ext.Viewport,
view = Ext.create(xtype);
//切换
me.animateActiveItem(view, {
type: 'slide',
direction: 'left'
});
},
//监控Viewport界面切换,切换时销毁旧视图
eActiveitemchange: function () {
var me = Ext.Viewport;
me.onAfter('activeitemchange',
function (t, value, oldValue, eOpts) {
if (oldValue) {
//强制销毁,防止销毁不完全引发错误
me.remove(oldValue, true);
}
});
},
//app初始化执行
inIt: function () {
this.addMessage();
this.overrideList();
this.overridePick();
this.overrideAjax();
this.eActiveitemchange();
},
//上传图片
openFileSelector: function () {
/*
*图片选择方式
*PHOTOLIBRARY:从相册中选取
*CAMERA:调用摄像头
*AVEDPHOTOALBUM:左右不明
*/
var source = navigator.camera.PictureSourceType.PHOTOLIBRARY;
/*
*图片返回格式
*DATA_URL:64位字符串
*FILE_URI:返回文件路径
*NATIVE_URI:返回系统路径 iOS:eg. assets-library:// Android: content://
*/
var destinationType = navigator.camera.DestinationType.FILE_URI;
/*
*媒体类型
*PICTURE:图片
*VIDEO:视频 始终返回FILE_URI格式
*ALLMEDIA:支持任意文件选择
*/
var mediaType = navigator.camera.MediaType.PICTURE;
var options = {
quality: 50, //图像质量[0-100]
destinationType: destinationType,
sourceType: source,
mediaType: mediaType
};
navigator.camera.getPicture(this.uploadFile, this.uploadBroken, options);
},
//图片选择失败
uploadBroken: function (message) {
this.showMessage(message, true);
},
//选择图片后上传
uploadFile: function (fileURI) {
var options = new FileUploadOptions();
options.fileKey = "userfile";
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
options.mimeType = "multipart/form-data";
options.chunkedMode = false;
ft = new FileTransfer();
var uploadUrl = encodeURI(config.imgUp);
this.showMessage('正在上传中,请等待...');
ft.upload(fileURI, uploadUrl, this.uploadSuccess, this.uploadFailed, options);
},
//文件上传成功
uploadSuccess: function (r) {
var res = Ext.decode(r.responseText);
this.showMessage(res.message, true);
},
//文件上传失败
uploadFailed: function (error) {
this.showMessage('图片上传失败...', true);
},
//输入检测
inputCheck: function (json, eMessage) {
for (var item in json) {
var message = eMessage[item];
if (Ext.isEmpty(json[item]) && message) {
this.showMessage(message, true);
return false;
}
}
return json;
}
}
});
用法:在app.json中引用该js文件,并在application的launch方法中调用myUtil.inIt()方法!
原文地址:http://www.cnblogs.com/mlzs/p/3302742.html