0.4.1 • Published 8 years ago
hpjs v0.4.1
#hpjs hpjs contains some tools help for javascrit development.
All the modules are wrote in nodejs's module style, using my gulp-defmod or browserify convert it for front-end.
base
Base functions.
validate chain
Combine mulitple validator as a chain, the error will always first one.
var validate = require('hpjs/validate')
var validator = require('hpjs/validator')
var length = validator.length(3, 10, 'wrong length')
var chars = validate.allCharsIn('abcdefghijklmnopqrstuvwxyz', 'bad chars')
var valid = validate.use(validate.use(length, chars))
console.log(length('abcdefghijklmnopqrstuvwxyz'))
console.log(chars('0123456789'))
console.log(valid('abcdefghijklmnopqrstuvwxyz0123456789', '111111', 'zzzz1'))
api
Simple library help for send http request to backend api server support both browser and nodejs.
- NodeJS(Express)
var api = request('hpjs/api')
// default backend on expressjs
function handle(req, resp) {
var a = api.sendPost('/user/info', req, {data:{}})
var process = function(res) {
console.log(res.statusCode, res.data)
}
a.success(process)
a.fail(process)
a.error(process)
a.any(process)
}
- Browser
<script type="text/javascript" src="/js/dist.js"></script>
<script type="text/javascript">
var process = function(res) {
console.log(res.statusCode, res.data)
}
var api = require('hpjs/api')
var a = api.sendPost('/user/info', {data:{}})
a.success(process)
a.fail(process)
a.error(process)
a.any(process)
</script>
jquery-tabswitch
Switch the active tab by location.path, depends on jquery's dom selector.
var tabswitch = require('hpjs/jquery-tabswitch')
var opts = null
tabswitch(opts).apply({
'/':'index',
'user':'',
'blog':''
})
Custom options with api.options
.
cookie
Porting from jquery-cookie.
var cookie = require('hpjs/cookie')
cookie.get('uid')
cookie.remove('uid')
cookie.set('uid', '12345')
cookie.config.defaults = {} // default cookie config
jquery-submit
Switch submit button's state.
var submit = require('hpjs/jquery-submit')
submit({
btn:'#submitBtn',
loadingText:'Loading',
errField:'#errMsg',
fields:[
'#username',
'#password',
{
field:'#content',
trim:false
}
],
callback: function(username, password, report) {
var err = ajax(user, password)
if (err) {
report(err)
} else {
location.href = '/'
}
}
})
router
a simple router support variables.
var router = require('hpjs/router')()
router.add('/user/:id/articles', function(params) {
console.log(params.param('id'), param.query('page'))
})
router.handle('/user/001/articles?page=1')
jquery-proute
router + pushState.
var proute = require('hpjs/jquery-proute')
var add = proute({
source:'a.proute',
target:'#proute-container',
})
add('/user/:id/articles', function(params, pushState, a) {
pushState(params.param('id'), a.text()) // content, title
})