0

下面我的代码的每个部分都创建了一个新的 handlebars.js 模板来调用“User Behance API”。如您所见,每个部分都有相似的代码,只有一个或两个变量。

有没有办法清理这段代码,并将这些单独的函数组合成一个?看起来就像很多代码,但我又是车把的新手。

// BEHANCE API INFO ---
var apiKey = 'ZLBxK9rEfHwJf9K0rmseNr2fS2gS2HJW';
var userID = 'creativemints';
var perPage = 10;
var behanceUserAPI = 'http://www.behance.net/v2/users/' + userID + '?callback=?&api_key=' + apiKey;
var behanceProjectAPI = 'http://www.behance.net/v2/users/' + userID + '/projects?callback=?&api_key=' + apiKey + '&per_page=' + perPage;

// BEHANCE - USER HEADER ---
(function () {
    function setUserTemplate() {
        var userData = JSON.parse(sessionStorage.getItem('behanceUser')),
            getTemplate = $('#userHead').html(),
            template = Handlebars.compile(getTemplate),
            result = template(userData);
        $('header').html(result);
    }
    if (sessionStorage.getItem('behanceUser')) {
        setUserTemplate();
    } else {
        $.getJSON(behanceUserAPI, function (user) {
            var data = JSON.stringify(user);
            sessionStorage.setItem('behanceUser', data);
            setUserTemplate();
        });
    }
})();

// BEHANCE - USER ABOUT ---
(function () {
    function setUserTemplate() {
        var userData = JSON.parse(sessionStorage.getItem('behanceUser')),
            getTemplate = $('#userAbout').html(),
            template = Handlebars.compile(getTemplate),
            result = template(userData);
        $('.about').html(result);
    }
    if (sessionStorage.getItem('behanceUser')) {
        setUserTemplate();
    } else {
        $.getJSON(behanceUserAPI, function (user) {
            var data = JSON.stringify(user);
            sessionStorage.setItem('behanceUser', data);
            setUserTemplate();
        });
    }
})();

// BEHANCE - USER FOOTER ---
(function () {
    function setUserTemplate() {
        var userData = JSON.parse(sessionStorage.getItem('behanceUser')),
            getTemplate = $('#userFoot').html(),
            template = Handlebars.compile(getTemplate),
            result = template(userData);
        $('footer').html(result);
    }
    if (sessionStorage.getItem('behanceUser')) {
        setUserTemplate();
    } else {
        $.getJSON(behanceUserAPI, function (user) {
            var data = JSON.stringify(user);
            sessionStorage.setItem('behanceUser', data);
            setUserTemplate();
        });
    }
})();

// BEHANCE - USER COPYRIGHT ---
(function () {
    function setUserTemplate() {
        var userData = JSON.parse(sessionStorage.getItem('behanceUser')),
            getTemplate = $('#userCopyright').html(),
            template = Handlebars.compile(getTemplate),
            result = template(userData);
        $('#copyright').html(result);
    }
    if (sessionStorage.getItem('behanceUser')) {
        setUserTemplate();
    } else {
        $.getJSON(behanceUserAPI, function (user) {
            var data = JSON.stringify(user);
            sessionStorage.setItem('behanceUser', data);
            setUserTemplate();
        });
    }
})();
4

1 回答 1

0

尚未对此进行测试,但您应该能够将代码简化为类似于此的代码

// BEHANCE API INFO ---
var apiKey = 'ZLBxK9rEfHwJf9K0rmseNr2fS2gS2HJW';
var userID = 'creativemints';
var perPage = 10;
var behanceUserAPI = 'http://www.behance.net/v2/users/' + userID + '?callback=?&api_key=' + apiKey;
var behanceProjectAPI = 'http://www.behance.net/v2/users/' + userID + '/projects?callback=?&api_key=' + apiKey + '&per_page=' + perPage;

// BEHANCE - USER HEADER ---
(function () {

function setUserTemplate(templateSelector, htmlSelector) {
    var userData = JSON.parse(sessionStorage.getItem('behanceUser')),
        getTemplate = $(templateSelector).html(),
        template = Handlebars.compile(getTemplate),
        result = template(userData);
    $(htmlSelector).html(result);
}

if (!sessionStorage.getItem('behanceUser')) {
    $.getJSON(behanceUserAPI, function (user) {
        var data = JSON.stringify(user);
        sessionStorage.setItem('behanceUser', data);
    });
}

setUserTemplate('#userHead','header');
setUserTemplate('#userAbout','.about');
setUserTemplate('#userFoot','footer');
setUserTemplate('#userCopyright','#copyright');
})();
于 2015-09-18T06:49:48.347 回答