JS如何封装一些列方法为一个对象的操作,然后集中管理这些操作,方便修改和调用
var Api = {
ajax:{
// 添加项目 旧!
add_project : function(pro_name, html, css, js,callback) {
$.post("/action/project/add", {
"v_code":User.v_code,
"pro_name" : pro_name,
"html" : html,
"css" : css,
"js" : js
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 添加代码 新!
add_code : function(code_name, html, css, js,callback) {
$.post("/action/code/add", {
"v_code":User.v_code,
"code_name" : code_name,
"html" : html,
"css" : css,
"js" : js
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 保存当前代码 旧!
update : function(id, html, css, js,sign,callback,force) {
$.post("/action/project/update", {
"v_code":User.v_code,
"id" : id,
"css" : css,
"js" : js,
"html" : html,
"sign":sign,
"force":force
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 保存当前代码 新!
update_code : function(id, html, css, js,sign,callback,force) {
$.post("/action/code/update", {
"v_code":User.v_code,
"id" : id,
"css" : css,
"js" : js,
"html" : html,
"sign":sign,
"force":force
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 存为新版本
new_version : function(id, html, css, js,callback) {
$.post("/action/project/new_version", {
"v_code":User.v_code,
"id" : id,
"css" : css,
"js" : js,
"html" : html
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// fork代码 旧!
fork : function(pro_id, ver, pro_name,callback) {
$.post("/action/project/fork", {
"v_code":User.v_code,
"pro_id" : pro_id,
"ver" : ver,
"pro_name" : pro_name
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// fork代码 新!
fork_code : function(id, code_name,callback) {
$.post("/action/code/fork", {
"v_code":User.v_code,
"id" : id,
"code_name" : code_name
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 删除指定版本的代码
delete_version : function(captcha,pro_id, ver,sign,callback,force) {
$.post("/action/project/delete_version", {
"captcha_":captcha,
"v_code":User.v_code,
"pro_id" : pro_id,
"ver" : ver,
"sign":sign,
"force":force
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
}, /**
* 删除代码,新!
*
*/
delete_code:function(captcha,id,callback){
$.post("/action/code/delete", {
"captcha_":captcha,
"v_code":User.v_code,
"id" : id
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
}, // 删除项目
delete_project : function(captcha,pro_id,callback) {
$.post("/action/project/delete_project", {
"captcha_":captcha,
"v_code":User.v_code,
"id" : pro_id
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 添加评论
add_comment : function(code_id, content,callback) {
$.post("/action/project/add_comment", {
"v_code":User.v_code,
"id" : code_id,
"content" : content
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 删除评论
delete_comment : function(comment_id,callback) {
$.post("/action/project/delete_comment", {
"v_code":User.v_code,
"id" : comment_id
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 投票,顶踩等
vote : function (code_id, type,callback) {
$.post("/action/project/vote", {
"v_code":User.v_code,
"id" : code_id,
"type" : type
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
}, login : function (name,callback) {
$.post("/action/ajax/login", {
"username" : name
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 登出
logout : function (callback) {
$.post("/action/ajax/logout",'uid='+User.user, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 通过id获取代码
getCode : function(id,callback) {
$.post("/action/api/getCode", {
"id" : id
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 项目重命名
project_rename : function(id,name,callback){
$.post("/action/project/rename", {
"v_code":User.v_code,
"pro_id" : id,
"name":name
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 代码重命名 旧!
code_rename : function(id,name,callback){
$.post("/action/project/rename_code", {
"v_code":User.v_code,
"code_id" : id,
"name":name
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 代码重命名 新!
code_rename : function(id,name,callback){
$.post("/action/code/rename", {
"v_code":User.v_code,
"id" : id,
"name":name
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 用户设置项,主题等
setting : function(name,value,callback){
$.post("/action/api/setting", {
"v_code":User.v_code,
"name" : name,
"value":value
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 发布代码 旧!
*/
publish : function(id,description,callback){
$.post("/action/project/post", {
"v_code":User.v_code,
"id" : id,
"description":description
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 发布代码 新!
*/
publish_code : function(id,description,callback){
$.post("/action/code/post", {
"v_code":User.v_code,
"id" : id,
"description":description
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 更新代码信息 旧!
update_info : function(id,name,description,callback){
$.post("/action/project/update_info", {
"v_code":User.v_code,
"id" : id,
"name" : name,
"description":description
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 更新代码信息 新!
update_code_info : function(id,name,description,callback){
$.post("/action/code/update_info", {
"v_code":User.v_code,
"id" : id,
"name" : name,
"description":description
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
}, // 收藏
favor : function(code_id,callback){
$.post("/action/project/favor", {
"v_code":User.v_code,
"id" : code_id
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
// 取消收藏
un_favor : function(favor_id,callback){
$.post("/action/project/un_favor", {
"v_code":User.v_code,
"id" : favor_id
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 添加url文件
*/
add_url_file : function(url,callback){
$.post("/action/file/add_url_file",{
"v_code":User.v_code,
"url":url
},function(msg){
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 删除文件
*/
delete_file : function(captcha,id,callback){
$.post("/action/file/delete_file", {
"captcha_":captcha,
"v_code":User.v_code,
"id" : id
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 添加建议
*/
add_advice : function(captcha,ident,email,content,callback){
$.post("/action/advice/add_advice",{
"captcha_":captcha,
"ident":ident,
"email":email,
"content":content
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 发送消息
*/
send_msg:function(receiver,content,callback){
$.post("/action/msg/sendMsg",{
"receiver":receiver,
"content":content
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 阅读消息
*/
read_msg:function(id,callback){
var params = '?';
$.each(id,function(i,cur){
params+="id="+cur;
if(i<id.length-1){
params+="&";
}
});
if(params=='?'){
params+="id="+id;
}
$.post("/action/msg/readMsg"+params,params, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 阅读所有的未读消息
*/
read_all_msg:function(type,callback){
$.post("/action/msg/readAllMsg",{
"v_code":User.v_code,
"type":type,
}, function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 将插件加到市场
*/
add_to_market:function(id,callback){
$.post("/action/plugin/add_to_market",{
"id":id
},function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 将代码更新到插件市场,id为code的id.
*/
update_to_market:function(id,callback){
$.post("/action/plugin/update_to_market",{
"id":id
},function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 从插件市场移出
*/
delete_from_market:function(id,callback){
$.post("/action/plugin/delete_from_market",{
"id":id
},function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 将代码设置为插件
*/
set_plugin:function(id,sys,callback){
$.post("/action/plugin/set_code_plugin",{
"id":id,
"sys":sys
},function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 审核插件
*/
check_plugin:function(id,callback){
$.post("/action/plugin/check",{
"id":id
},function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 取消审核插件
*/
uncheck_plugin:function(id,callback){
$.post("/action/plugin/uncheck",{
"id":id
},function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 加到广场
*/
add_to_square:function(id,callback){
$.post("/action/square/add",{
"id":id
},function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 更新到广场
*/
update_to_square:function(id,callback){
$.post("/action/square/update",{
"id":id
},function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 添加分类
*/
add_catalog:function(name,callback){
$.post("/action/catalog/add",{
"name":name
},function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 分类重命名
*/
rename_catalog:function(name,id,callback){
$.post("/action/catalog/rename",{
"name":name,
"id":id
},function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 删除分类,该分类必须没有代码
*/
delete_catalog:function(id,callback){
$.post("/action/catalog/delete",{
"id":id
},function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 移动到分类
*/
move_to_catalog:function(code_id,catalog_id,callback){
$.post("/action/catalog/move_to",{
"id":code_id,
"catalog":catalog_id
},function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
/**
* 设置代码类型
*/
set_code_type:function(id,code_type,type,callback){
$.post("/action/code/set_code_type",{
"id":id,
"code":code_type,
"type":type
},function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
},
less_compile:function(less,callback){
$.post("/action/ajax/less_compile",less,function(msg) {
if(typeof callback != 'undefined')return callback(msg);
});
}
}
};
这是一个例子,那么从例子中,我们可以学习到,如何封装一系列方法到一个对象中。
下面是如何调用已经封装好的方法,居然是如此的简练:
$(function() {
if (typeof isIE6 != "undefined") {
$(".logo").attr({
"class" : "ie6_logo"
})
}
$(window).bind("resize", function() {
resize();
})
$(".comment").click(function() {
$(".fork").attr("class", "nofocus fork");
$(this).attr("class", "focus comment");
$(".detail_comment").show();
$(".detail_fork").hide();
});
$(".fork").click(function() {
$(".comment").attr("class", "nofocus comment");
$(this).attr("class", "focus fork");
$(".detail_comment").hide();
$(".detail_fork").show();
});
$("#logout").click(function() {
logout();
});
$(".prelogin").click(function() {
new $.Zebra_Dialog($("#user_login").html(), {
'title' : '登录方式',
'modal' : true,
'width' : 460,
'type' : false,
'buttons' : [ {
caption : '取消'
} ]
}).show();
/*
* var url; //转向网页的地址; var name; //网页名称,可为空; var iWidth; //弹出窗口的宽度; var
* iHeight; //弹出窗口的高度; var iTop =
* (window.screen.availHeight-30-iHeight)/2; //获得窗口的垂直位置; var iLeft =
* (window.screen.availWidth-10-iWidth)/2; //获得窗口的水平位置;
* window.open(url,name,'height='+iHeight+',,innerHeight='+iHeight+',width='+iWidth+',innerWidth='+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');
*/
});
resize();
$("button.detail_comment_button").click(function() {
addComment();
});
$("textarea[name='content']").keydown(function(event) {
if (event.ctrlKey && event.keyCode == 13) {
addComment();
}
});
$("#view_scale").change(function() {
var sc = $(this).val();
var w = '100%';
var h = '100%';
var t = 'scale(1)';
if (sc == '0.5') {
w = '200%';
h = '200%';
t = 'scale(0.5)';
} else if (sc == '2') {
t = 'scale(2)';
}
$("#code_detail").css({
'width' : w,
'height' : h,
'-webkit-transform' : t,
'transform' : t
});
});
$("#show_gist").mouseenter(function(){
show_gist();
});
}); var resize = function() {
var width = $(window).width();
var cw = $(".comment").width();
var w = width;
if (w < 1000)
w = 1000;
w = w - 300 - 60;
$(".detail_mainContent_left,.detail_comment_input textarea")
.css("width", w);
$(".detail_wrapper").css("width", width < 1000 ? 960 : width - 40);
$(".fork").css("width", w - cw - 43);
}
function logout() {
Api.ajax.logout(function(msg) {
location.href = RURL;
});
}
function login(op) {
var url = "http://runjs.cn/action/openid/before_login?op=" + op; // 转向网页的地址;
var name = "用" + op + "登录RunJS"; // 网页名称,可为空;
var iWidth = 800; // 弹出窗口的宽度;
var iHeight = 600; // 弹出窗口的高度;
var iTop = (window.screen.availHeight - 30 - iHeight) / 2; // 获得窗口的垂直位置;
var iLeft = (window.screen.availWidth - 10 - iWidth) / 2; // 获得窗口的水平位置;
window
.open(
url,
name,
'height='
+ iHeight
+ ',,innerHeight='
+ iHeight
+ ',width='
+ iWidth
+ ',innerWidth='
+ iWidth
+ ',top='
+ iTop
+ ',left='
+ iLeft
+ ',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');
}
function addComment() {
var ctn = $("textarea[name='content']");
var content = ctn.val();
if (content.length == 0 || content == "觉得怎么样?赶紧说几句"){
alert("请输入评论内容");
return;
}
Api.ajax
.add_comment(
Code.id,
content,
function(msg) {
var msg = eval("(" + msg + ")");
if (msg.error) {
alert(msg.msg);
return false;
}
var comment = $('<li id="comment_'
+ msg.id
+ '">'
+ '<img src="'
+ User.portrait
+ '" width="48" height="48">'
+ '<p><a href="'
+ (typeof User.space == "undefined" ? 'javascript:void(0);'
: User.space)
+ '">'
+ User.name
+ '</a><span class="time">1分钟前</span><span class="delete"><a href="javascript:removeComment('
+ msg.id + ');">删除</a></span></p>' + '<p>'
+ msg.content + '</p>' + '</li>');
comment.hide();
$(".detail_comment_list").prepend(comment);
comment.show(500);
$(".zeroComment").remove();
ctn.val("");
});
}
function removeComment(id) {
if (confirm("确认删除此条评论?")) {
Api.ajax
.delete_comment(
id,
function(msg) {
var msg = eval("(" + msg + ")");
if (msg.error) {
alert(msg.msg);
return false;
}
$("#comment_" + id)
.hide(
500,
function() {
$(this).remove();
if ($(".detail_comment_list li").length == 0) {
$(".detail_comment_list")
.prepend(
'<li class="zeroComment" style="text-align:center;">还没有人评论哦,赶紧抢个沙发吧~~</li>');
}
});
});
}
} function viewAll(t) {
$(".detail_otherProject li").show();
$("#view_all_li").remove();
}
function add_to_square(id) {
Api.ajax.add_to_square(id, function(msg) {
var msg = eval("(" + msg + ")");
if (msg.error) {
alert(msg.msg);
return false;
} else {
alert("添加成功");
location.reload();
}
});
}
function update_to_square(id) {
Api.ajax.update_to_square(id, function(msg) {
var msg = eval("(" + msg + ")");
if (msg.error) {
alert(msg.msg);
return false;
} else {
alert("更新成功");
location.reload();
}
});
} function delete_from_square(id) {
$.post("/action/square/delete", {
"id" : id
}, function(msg) {
var msg = eval("(" + msg + ")");
if (msg.error) {
alert(msg.msg);
return false;
} else {
alert("去除成功");
location.reload();
}
});
}
function love(id) {
Api.ajax.vote(id, 1, function(m) {
m = eval("(" + m + ")");
if (m.error) {
alert(m.msg);
} else {
var ar = $(".detail_interactArea_like a");
ar.css({
'background-image' : 'url(../img/liked.gif)'
});
ar.attr("href", "javascript:void(0);");
love_count++;
ar.html("已喜欢<span>(" + love_count + ")</span>");
}
});
}
function show_gist(){
$("#gist_span").show();
$("#gist_input").focus().select();
}
JS如何封装一些列方法为一个对象的操作,然后集中管理这些操作,方便修改和调用的更多相关文章
- JS中的对象和方法简单剖析
众所周知,在js中对象就是精髓,不理解对象就是不理解js. 那么什么事js中的对象呢? 在js中,几乎一切皆对象: Boolean ,String,Number可以是对象(或者说原生数据被认作对象): ...
- js jq封装ajax方法
json文本格式 { "userInfo":[ {name:"admin",password:"123"}, {name:"adm ...
- JS实现继承,封装一个extends方法
父类 function Person(name,age){ this.name = name; this.age = age; } Person.prototype = { eat:function( ...
- JS 对象封装的常用方式
JS是一门面向对象语言,其对象是用prototype属性来模拟的,下面,来看看如何封装JS对象. 常规封装 function Person (name,age,sex){ this.name = na ...
- react request.js 函数封装
1.request.js 函数封装 import { Toast } from 'antd-mobile'; import axios from 'axios'; import store from ...
- Blazor组件自做八 : 使用JS隔离封装屏幕键盘kioskboard.js组件
1. 运行截图 演示地址 2. 在文件夹wwwroot/lib,添加kioskboard子文件夹,添加kioskboards.js文件 2.1 常规操作,懒加载js库, export function ...
- JS中 call() 与apply 方法
1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call ...
- JSF页面中使用js函数回调后台bean方法并获取返回值的方法
由于primefaces在国内使用的并不是太多,因此,国内对jsf做系统.详细的介绍的资料很少,即使有一些资料,也仅仅是对国外资料的简单翻译或者是仅仅讲表面现象(皮毛而已),它们的语句甚至还是错误的, ...
- js中this和回调方法循环-我们到底能走多远系列(35)
我们到底能走多远系列(35) 扯淡: 13年最后一个月了,你们在13年初的计划实现了吗?还来得及吗? 请加油~ 主题: 最近一直在写js,遇到了几个问题,可能初入门的时候都会遇到吧,总结下. 例子: ...
随机推荐
- 测试用(编写优质嵌入式C程序)
注:相比于Word,如果使用CSDN自带编辑器写出结构清晰的文档,需要花费更多的时间,所以我尝试将我写的一些word文档转换为图片发布,这样就可以保持原来的结构.字体,可以获得更好的可读性.图片的分辨 ...
- linux系统配置文件和用户配置文件及其作用
我的博客:www.while0.com /etc/issue 未登陆时控制台显示的文字 /etc/issue.net 远程登陆时控制台显示的文字 /etc/motd 用户登陆时显示的文字 这里先提供两 ...
- perl 面向对象demo
Vsftp:/root/perl/17# cat Critter.pm package Critter; sub new { my $self = {}; my $invocant = shift; ...
- ☀【CSS3】形状
CSS3shapeshttp://www.css3shapes.com/ <!DOCTYPE html> <html lang="zh-CN"> <h ...
- 【转】Android中自定义控件的步骤
原文网址:http://blog.csdn.net/lianchen/article/details/48038969 Android开发中难免遇到需要自定义控件的需求,有些是产品的要求在Androi ...
- CentOS升级内核的方法
升级前的内核版本为:2.6.32-431.el6.x86_64 升级后的内核版本为:3.10.101-1.el6.elrepo.x86_64 升级方法: 1.导入key rpm --import ht ...
- linux gcc 编译时头文件和库文件搜索路径
一.头文件 gcc 在编译时寻找所需要的头文件 : ※搜寻会从-I开始 ※然后找gcc的环境变量 C_INCLUDE_PATH,CPLUS_INCLUDE_PATH,OBJC_INC ...
- 判斷作業系統為 64bit 或 32bit z
有時我們在開發Windows 桌面應用程式時,會發生一些弔詭的事情,作業系統位元數就是一個蠻重要的小細節,若您寫的應用程式在Windows 的32bit 作業系統上可以完美的運行,但不見得在64bit ...
- Delphi 预编译指令 的用法
A.3 使用条件编译指令条件编译指令是非常重要的编译指令,他控制着在不同条件下(例如,不同的操作系统)产生不同的代码.条件编译指令是包含在注释括号之内的,如下表所示. ...
- zoj 3620 Escape Time II
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4744 Escape Time II Time Limit: 2 Seconds ...