Skip to content

Instantly share code, notes, and snippets.

@boboidream
Created March 2, 2017 02:34
Show Gist options
  • Save boboidream/dd311b578f05859dbf157c3e4817097f to your computer and use it in GitHub Desktop.
Save boboidream/dd311b578f05859dbf157c3e4817097f to your computer and use it in GitHub Desktop.
// 三个为单独接口
function renderDept(userInfo) {
var bgs = [],
dps = [],
centers = [],
dept_compiled = '',
dept_html = ''
var getBg = function() {
return $.getJSON('/api/base/dept')
.done(function(res) {
bgs = res
})
.fail(function(res) {
console.err(res)
})
}
var getDept = function(bg_id) {
return $.getJSON('/api/base/dept?bg_id=' + bg_id)
.done(function(res) {
dps = res
})
.fail(function(res) {
console.error(res);
});
}
var getCenter = function(dept_id) {
return $.getJSON('/api/base/dept?dept_id=' + dept_id)
.done(function(res) {
centers = res
})
.fail(function(res) {
console.error(res);
})
}
$.when(getBg(), getDept(userInfo.bg_id), getCenter(userInfo.dept_id)).then(function() {
render()
})
// 监听 bg
$(document).on('change', 'select[name=bg]', function() {
userInfo.bg_id = $(this).val()
$.when(getDept(userInfo.bg_id)).then(function() {
userInfo.dept_id = dps[0].id
$.when(getCenter(userInfo.dept_id)).then(function() {
render()
})
})
})
// 监听 dept
$(document).on('change', 'select[name=dp]', function() {
userInfo.dept_id = $(this).val()
$.when(getCenter(userInfo.dept_id)).then(function() {
render()
})
})
function render() {
dept_compiled = Template($('#dept_template').text())
dept_html = dept_compiled({
bgs: bgs,
dps: dps,
centers: centers,
user: userInfo
})
$('#dept').html(dept_html)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment