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,遇到了几个问题,可能初入门的时候都会遇到吧,总结下. 例子: ...
随机推荐
- Delphi XML-RPC 中文乱码解决方法
http://download.csdn.net/user/csm2432/uploads/2
- 转:二十一、详细解析Java中抽象类和接口的区别
转:二十一.详细解析Java中抽象类和接口的区别 http://blog.csdn.net/liujun13579/article/details/7737670 在Java语言中, abstract ...
- andriod系统裁剪心得
亲们,,有人做过 将android系统总内存减少,并保持系统的稳定运行 方面的么?...比如将512M的内存换成256M,系统依然稳定运行,, 我目前,从三个方面下手,,,1.删减系统中不需要使用的a ...
- java 包中的一款经典的singleton模式
/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Versi ...
- VS(Microsoft Visual Studio2010)工具打开项目所需的应用程序,出现未安装(.csproj)的应用程序的解决办法
打开这个文件的话,从列表中选择打开方式的时候,看看选择打开项目文件的打开方式是不是visual studio,如果是Micrisoft visual studio version selector.那 ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- DevExpress控件学习总结 z
1.Navigation & Layout 1.1 Bar Manager 如果想在窗体或用户控件(user control)上添加工具条(bars)或弹出菜单(popup menus),我们 ...
- sql getdate() 时间格式设置
Sql Server 中一个非常强大的日期格式化函数常用: Select CONVERT(varchar(100), GETDATE(), 23): 2006-05-16 Select CONVE ...
- 统计难题 HDOJ--2222
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Redis主从实现原理分析 [转]
原文地址:http://blog.sina.com.cn/s/blog_7530db6f0100vegl.html 一, 实现原理图 (1)Slave服务器连接到Master服务器. (2)Slave ...