Created
January 4, 2017 03:42
-
-
Save rming/fc3f6eb7a8550a61730b05c6ec877d28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*genarate javascript namspace function*/ | |
var _ns= {}; | |
_ns.genNameSpace= function(path, loseMe){ | |
var reg = /^[_$a-z]+[_$a-z0-9]*/i; | |
var nsAr = path.split('.'); | |
var stringCommond = ""; | |
var stringNS = ""; | |
var n = loseMe ? nsAr.length - 1 : nsAr.length; | |
for (var i = 0; i < n; i++){ | |
if(!reg.test(nsAr[i])) { | |
throw new Error("namespace error" + nsAr[i] + ""); | |
return ; | |
} | |
if (i != 0) stringNS += "."; | |
stringNS += nsAr[i]; | |
stringCommond += "if(typeof(" + stringNS + ")=='undefined') " + stringNS + "=new Object();else " + stringNS + ";"; | |
} | |
if (stringCommond != "") { | |
return eval(stringCommond); | |
} | |
return {}; | |
}; | |
var _util = _ns.genNameSpace("ns.util"); | |
(function(ns){ | |
ns.run = function() { | |
} | |
ns.baiduHmScript = function(hmId) { | |
var _hmt = _hmt || []; | |
var _hmId = hmId||""; | |
(function() { | |
var hm = document.createElement("script"); | |
hm.src = "//hm.baidu.com/hm.js?"+_hmId; | |
var s = document.getElementsByTagName("script")[0]; | |
s.parentNode.insertBefore(hm, s); | |
})(); | |
} | |
})(_util); | |
_util.run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//<script src="jquery.min.js" type="text/javascript"></script> | |
//已经手动引入jquery | |
//login.js文件内容 | |
var login = _ns.genNameSpace("ns.login"); | |
(function(ns){ | |
ns.run = function() { | |
$('#login-btn').click(ns.doLogin); | |
$('#register-btn').click(ns.doLogin); | |
} | |
ns.pageInit = function() { | |
//page init actions | |
$('#page-tittle').html('<h1>登陆页面</h1>'); | |
$('body').css({background:'#F00'}); | |
} | |
ns.doLogin = function(e) { | |
var form = $(this).parents('form#login-form'); | |
//var username = form.find('.username').val(); | |
//var password = form.find('.password').val(); | |
$.post(form.attr('action'),form.serialize(),function(data){ | |
console.log(data); | |
}) | |
} | |
ns.alert = function(str) { | |
var str = str||''; | |
alert(str); | |
} | |
ns.run(); | |
})(login); | |
//在页面中或者其他文件中,方法调用 | |
login.alert('login.alert: hello!') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment