Javascript Prototype reuse same method -
i have js file
function apiaccess(){ this.loadscreen = function(){ var loadscreen = $('#loadscreen'); if(loadscreen.html() == undefined){ loadscreen = '<div id="loadscreen" style="display: none;width: 100%; height: 100%; top: 0pt;left: 0pt;">' + '<div id="loadscr" style="filter: alpha(opacity = 65); z-index: 9999;border: medium none; margin: 0pt; padding: 0pt; width: 100%; height: 100%; top: 0pt;left: 0pt; background-color: rgb(0, 0, 0); opacity: 0.2; cursor: wait; position: fixed;"></div>' + '<div id="loader" style="z-index: 10000; position: fixed; padding: 0px; margin: 0px;width: 30%; top: 40%; left: 35%; text-align: center;cursor: wait; ">' + '<img src="img/ajax-loader.gif" alt="loading" /></div></div>'; $(document.body).append(loadscreen); } }; this.apicall = function(url, params, method, callback){ this.loadscreen(); var postdata = null; if(params != null){ postdata = json.stringify(params); } if(url.tolowercase().indexof("http") < 0){ url = "http://" + url; } $('#loadscreen').show(function(){ $.ajax({ url: url, async: true, type: method, data: postdata, success: function(data){ $('#loadscreen').hide(); callback(data); }, error:function(data){ alert("failure"); return false; } }); }); }; } function domain(reqcallback){ this.url = 'http://beta.test123.net/api/domain'; this.params = null; this.method = 'get'; this.callback = function(data){ setcookie("domain", data); if(typeof reqcallback != null){ reqcallback(data); } }; this.request = this.apicall(this.url, this.params, this.method, this.callback); } domain.prototype = new apiaccess; function login(usermail, pass, reqcallback){ var domainurl = getcookie("domain"); if(domainurl == null) return false; else domainurl += '/api/login'; this.url = domainurl; this.params = {"email": usermail, "password": password}; this.method = 'post'; this.callback = function(data){ setcookie("login", data); if(typeof reqcallback != null){ reqcallback(data); } }; this.request = this.apicall(this.url, this.params, this.method, this.callback); } login.prototype = new apiaccess;
if see method this.request = this.apicall(this.url, this.params, this.method, this.callback);
repeated everytime. wish placed in apiaccess function. can please suggest can done.
i use in html this
$(document).ready(function(){ var domain = new domain(function(data){ alert(data); }); domain.request; });
i'm not sure if asking for, important part of code below have used object.create() instead of object prototype "inherit" base class if want learn why? visit https://developer.mozilla.org/en-us/docs/javascript/reference/global_objects/object/create
hope helps :)
//base class var apiaccess = { loadscreen : function () { var loadscreen = $('#loadscreen'); if (loadscreen.html() == undefined) { loadscreen = '<div id="loadscreen" style="display: none;width: 100%; height: 100%; top: 0pt;left: 0pt;">' + '<div id="loadscr" style="filter: alpha(opacity = 65); z-index: 9999;border: medium none; margin: 0pt; padding: 0pt; width: 100%; height: 100%; top: 0pt;left: 0pt; background-color: rgb(0, 0, 0); opacity: 0.2; cursor: wait; position: fixed;"></div>' + '<div id="loader" style="z-index: 10000; position: fixed; padding: 0px; margin: 0px;width: 30%; top: 40%; left: 35%; text-align: center;cursor: wait; ">' + '<img src="img/ajax-loader.gif" alt="loading" /></div></div>'; $(document.body).append(loadscreen); } }, apicall : function (url, params, method, callback) { this.loadscreen(); var postdata = null; if (params != null) { postdata = json.stringify(params); } if (url.tolowercase().indexof("http") < 0) { url = "http://" + url; } $('#loadscreen').show(function () { $.ajax({ url: url, async: true, type: method, data: postdata, success: function (data) { $('#loadscreen').hide(); callback(data); }, error: function (data) { alert("failure"); return false; } }); }); }, //added base class url : null, params : null, method : null, callback : null, request : function(){ //todo validate url, params , method here apicall(this.url, this.params, this.method, this.callback); } } var domain = function(reqcallback) { var obj = object.create(apiaccess); //obj.prototype = apiaccess; obj.url = 'http://beta.test123.net/api/domain'; obj.params = null; obj.method = 'get'; obj.callback = function (data) { setcookie("domain", data); if (typeof reqcallback != null) { reqcallback(data); } }; return obj; } var login = function (usermail, password, reqcallback) { var domainurl = getcookie("domain"); if (domainurl == null){ return false; } else{ domainurl += '/api/login'; } var obj = object.create(apiaccess); //obj.prototype = apiaccess; obj.url = domainurl; obj.params = { "email": usermail, "password": password }; obj.method = 'post'; obj.callback = function (data) { setcookie("login", data); if (typeof reqcallback != null) { reqcallback(data); } } return obj; } //code below testing function getcookie(str){ return 'test'; } console.log( new domain(function(data){alert(data);}), //domain new login( //loging 'user', 'passwd', function(data){alert(data);} ) )
Comments
Post a Comment