JS如何封装一些列方法为一个对象的操作,然后集中管理这些操作,方便修改和调用
2017-01-09 13:32
1021 查看
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); }); } } };
View Code
这是一个例子,那么从例子中,我们可以学习到,如何封装一系列方法到一个对象中。
下面是如何调用已经封装好的方法,居然是如此的简练:
$(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(); }
相关文章推荐
- eclipse 中main()函数中的String[] args如何使用?通过String[] args验证账号密码的登录类?静态的主方法怎样才能调用非static的方法——通过生成对象?在类中制作一个方法——能够修改对象的属性值?
- 如何遍历一个JS对象的属性和方法
- 关于“如何做到在一个类内不通过创建另一个类的对象,却调用另一个类的方法”的问题
- Jquery如何序列化form表单数据为JSON对象 C# ADO.NET中设置Like模糊查询的参数 从客户端出现小于等于公式符号引发检测到有潜在危险的Request.Form 值 jquery调用iframe里面的方法 Js根据Ip地址自动判断是哪个城市 【我们一起写框架】MVVM的WPF框架(三)—数据控件 设计模式之简单工厂模式(C#语言描述)
- 如何初始化(新建)一个方法 ,如何方法逆向调用对象
- js调用重复定义的一个方法,如何执行
- 封装了一个关于python操作fastDFS的类,方便调用
- 写一个方法进行各种属性的更新,而不要每个界面设置改变都改变一个属性。就是点击应用或确定按钮时调用这个更新属性的方法,遍历所有(控制对象属性的)界面控件的状态(值),进行属性修改和刷新。
- 使用MVC中的HtmlHelper对象的ActionLink方法,实现一个Javascript:void(0)效果,去调用一个JS方法
- 利用java基础写的一个简单的小游戏,没有UI界面,只是涉及到类的封装,方法的调用,方便日后复习用
- 利用js的闭包原理做对象封装及调用方法
- 封装一个方法实现js内置对象array的indexOf()方法
- JS +XML +Jquery 实现三级联动菜单,自己封装的一个对象方便使用
- 面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候一个一个依次调用就可以了;面向对象是把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描叙某个事物在整个解决问题的步骤中的行为(转)
- ios 中调用函数的方法是消息传递,这个和普通的函数调用的区别是,你可以随时对一个对象传递任何消息,而不需要在编译的时候声明这些方法。所以Objective-C可以在runtime的时候
- AS、Flex如何循环调用一个对象的属性、方法
- js中如何判读一个对象没有任何属性和方法
- js中关于map的增、删、改、查操作及其js中如何建立一个对象
- JS 实现 创建类、继承、方法添加、对象克隆、数组封装操作
- 如何用js实现,在一个页面有个输入文档框,然后点击查询按扭后,调用IE的查询功能在当前页面进行查询