@CSS魔法
2015. 06. 18.
(30~45 分钟)
(这里给大家上真实代码)
var bindGetCodeAction = function () {
$('#getMobileCode').click(function (ev) {
countdown()
})
}
if ($('input[name=mobileCode]').length) {
bindGetCodeAction()
}
$('.form-submit').click(function (ev) {
$.ajax({
type: 'post',
url: '/foo/bar',
success: function (data) {
if (data.status) {
$('form').submit()
} else {
switch (data.code) {
case '10404':
//...
break
case '10403':
//...
break
}
}
}
})
})
var countdown = function () {
//...
}
var bindGetCodeAction = function () {
$('#getMobileCode').click(function (ev) {
countdown()
})
}
if ($('input[name=mobileCode]').length) {
bindGetCodeAction()
}
$('.form-submit').click(function (ev) {
$.ajax({
type: 'post',
url: '/foo/bar',
success: function (data) {
if (data.status) {
$('form').submit()
} else {
switch (data.code) {
case '10404':
//...
break
case '10403':
//...
break
}
}
}
})
})
var countdown = function () {
//...
}
(可能是为了避免太多全局变量)
var bindGetCodeAction = function () {
$('#getMobileCode').click(function (ev) {
countdown()
})
}
if ($('input[name=mobileCode]').length) {
bindGetCodeAction()
}
$('.form-submit').click(function (ev) {
$.ajax({
type: 'post',
url: '/xxx/yyy',
success: function (data) {
if (data.status) {
$('form').submit()
} else {
switch (data.code) {
case '10404':
//...
break
case '10403':
//...
break
}
}
}
})
})
var countdown = function () {
//...
}
var fabu = {
//...
}
var fabu = {
init: function () {
//...
},
//...
}
fabu.init()
var fabu = {
init: function () {
this._bind()
},
_bind: function () {
var _this = this
if ($('input[name=mobileCode]').length) {
$('#getMobileCode').click(function (ev) {
_this.countdown()
})
}
$('.form-submit').click(function (ev) {
$.ajax({
//...
//...
//...
})
})
},
countdown: function () {
//...
}
}
fabu.init()
var fabu = {
init: function () {
this._bind()
},
_bind: function () {
var _this = this
if ($('input[name=mobileCode]').length) {
$('#getMobileCode').click(function (ev) {
_this.countdown()
})
}
$('.form-submit').click(function (ev) {
_this._ajax()
})
},
_ajax: function () {
$.ajax({
//...
//...
//...
})
},
countdown: function () {
//...
}
}
fabu.init()
var fabu = {
init: function () {
this._bind()
},
_bind: function () {
//...
},
_ajax: function () {
var _this = this
$.ajax({
type: 'post',
url: '/xxx/yyy',
success: function (data) {
_this._success(data)
}
})
},
_success: function (data) {
//...
//...
//...
},
countdown: function () {...}
}
fabu.init()
var fabu = {
init: function () {
this._bind()
},
_bind: function () {
//...
},
_ajax: function () {
//...
},
_success: function (data) {
if (data.status) {
$('form').submit()
} else {
switch (data.code) {
case '10404':
this._caseWrongInput()
break
case '10403':
this._caseNeedRecheck()
break
}
}
},
_caseWrongInput: function () {...},
_caseNeedRecheck: function () {...},
countdown: function () {...}
}
fabu.init()
var fabu = {
init: function () {
this._getElem()
this._bind()
},
_getElem: function () {
this.$btnSubmit = $('.form-submit')
this.$btnGetMobileCode = $('#getMobileCode')
this.$inputMobileCode = $('input[name=mobileCode]')
},
_bind: function () {
var _this = this
if (this.$inputMobileCode.length) {
this.$btnGetMobileCode.click(function (ev) {
_this.countdown()
})
}
this.$btnSubmit.click(function (ev) {
_this._ajax()
})
},
_ajax: function () {
//...
},
_success: function (data) {...},
_caseWrongInput: function () {...},
_caseNeedRecheck: function () {...},
countdown: function () {...}
}
fabu.init()
var app = {
CODE_WRONG_INPUT: '10404',
CODE_NEED_RECHECK: '10403',
init: function () {
this._getElem()
this._bind()
},
_getElem: function () {...},
_bind: function () {...},
_ajax: function () {...},
_success: function (data) {
if (data.status) {
$('form').submit()
} else {
switch (data.code) {
case this.CODE_WRONG_INPUT:
this._caseWrongInput()
break
case this.CODE_NEED_RECHECK:
this._caseNeedRecheck()
break
}
}
},
_caseWrongInput: function () {...},
_caseNeedRecheck: function () {...},
countDown: function () {...}
}
app.init()
var fabu = {
CODE_WRONG_INPUT: '10404',
CODE_NEED_RECHECK: '10403',
init: function () {
this._getElem()
this._bind()
},
_getElem: function () {...},
_bind: function () {...},
_ajax: function () {...},
_success: function (data) {...},
_caseWrongInput: function () {...},
_caseNeedRecheck: function () {...},
countdown: function () {...}
}
fabu.init()
var bindGetCodeAction = function () {
$('#getMobileCode').click(function (ev) {
countdown()
})
}
if ($('input[name=mobileCode]').length) {
bindGetCodeAction()
}
$('.form-submit').click(function (ev) {
$.ajax({
type: 'post',
url: '/foo/bar',
success: function (data) {
if (data.status) {
$('form').submit()
} else {
switch (data.code) {
case '10404':
//...
break
case '10403':
//...
break
}
}
}
})
})
var countdown = function () {
//...
}
var app = {}
// 注册功能
app.reg = {
//...
}
// 登录功能
app.login = {
//...
}
// 发布功能
app.fabu = {
//...
}
// 初始化
app.reg.init()
app.login.init()
app.fabu.init()
每个功能都要占用一个全局变量?
树干只需要一个。
// 发布的初始化方法
fabu.init()
// 发布的倒计时接口
fabu.countdown()
接近自然语言:
// 初始化
app.reg.init()
app.login.init()
app.fabu.init()
// 涉及的 DOM 元素
fabu.$btnSubmit
// 公开接口
fabu.countdown()
// 内部方法
fabu._success(data)
// 更细粒度的内部方法
fabu._caseWrongInput()
fabu._caseNeedRecheck()
所有变量、方法均可在全局访问。
“模块模式” 的各种变种。
有前面命名空间的基础很容易理解和掌握。
void function () {
'use strict'
//...
}()
void function () {
'use strict'
var CODE_WRONG_INPUT = '10404'
var CODE_NEED_RECHECK = '10403'
var $btnSubmit, $btnGetMobileCode, $inputMobileCode
function init() {...}
function _getElem() {...}
function _bind() {...}
function _ajax() {...}
function _success(data) {...}
function _caseWrongInput() {...}
function _caseNeedRecheck() {...}
function countdown() {...}
}()
void function () {
'use strict'
var CODE_WRONG_INPUT = '10404'
var CODE_NEED_RECHECK = '10403'
var $btnSubmit, $btnGetMobileCode, $inputMobileCode
function init() {...}
function _getElem() {...}
function _bind() {...}
function _ajax() {...}
function _success(data) {...}
function _caseWrongInput() {...}
function _caseNeedRecheck() {...}
function countdown() {...}
var exports = {
init: init,
countdown: countdown
}
}()
var app = {}
void function () {
'use strict'
var CODE_WRONG_INPUT = '10404'
var CODE_NEED_RECHECK = '10403'
var $btnSubmit, $btnGetMobileCode, $inputMobileCode
function init() {...}
function _getElem() {...}
function _bind() {...}
function _ajax() {...}
function _success(data) {...}
function _caseWrongInput() {...}
function _caseNeedRecheck() {...}
function countdown() {...}
var exports = {
init: init,
countdown: countdown
}
// exports
app.fabu = exports
}()
app.fabu.init()
按需暴露。
var app = {}
void function () {
'use strict'
var CODE_WRONG_INPUT = '10404'
var CODE_NEED_RECHECK = '10403'
var $btnSubmit, $btnGetMobileCode, $inputMobileCode
function init() {...}
function _getElem() {...}
function _bind() {...}
function _ajax() {...}
function _success(data) {...}
function _caseWrongInput() {...}
function _caseNeedRecheck() {...}
function countdown() {...}
var fabu = {
init: init,
countdown: countdown
}
// debug
fabu._caseWrongInput = _caseWrongInput
fabu._caseNeedRecheck = _caseNeedRecheck
// exports
app.fabu = fabu
}()
定义模块
// fabu.js
'use strict'
var CODE_WRONG_INPUT = '10404'
var CODE_NEED_RECHECK = '10403'
var $btnSubmit, $btnGetMobileCode, $inputMobileCode
function init() {...}
function _getElem() {...}
function _bind() {...}
function _ajax() {...}
function _success(data) {...}
function _caseWrongInput() {...}
function _caseNeedRecheck() {...}
function countdown() {...}
module.exports = {
init: init,
countdown: countdown
}
// page.js
var fabu = require('fabu')
fabu.init()
使用模块
Thank You!