Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm wondering if it is possible or if it is correct to proceed in such a way:

var initiate_shop = function(shop_navigation, shop_container_id, items_per_page) {
    return $(shop_navigation).jPages({
        containerID     : shop_container_id,
        first           : false,
        previous        : false,
        next            : false,
        last            : false,
        midRange        : 15,
        perPage         : items_per_page,
        animation       : "fadeInUp",
        links           : "blank",
        keyBrowse       : true,
        scrollBrowse    : false,
        callback        : function(pages){
            current_page = pages.current;
            total_pages = pages.count;
        }
    });
};

If the above isn't correct what could be a better way to write it?

share|improve this question

1 Answer

up vote 2 down vote accepted

Looks good to me, if you always want those 3 params and never want to send anything else.

Otherwise, you could improve it by receiving the non-mandatory params in an options object:

"use strict";
var initiate_shop = function(container_id, options) {
    var default_settings = {
            containerID     : container_id,
            first           : false,
            perPage         : 5,
            '...'           : '...',
            callback        : function(pages){
                current_page = pages.current;
                total_pages = pages.count;
            }
        },
        settings = $.extend(default_settings, options);
    return $(shop_navigation).jPages(settings);
};
// ...
initiate_shop('outputCell', {
    perPage:     10,
    first:       true
});

Related (hows 3 ways to assign default values to parameters):
http://stackoverflow.com/a/3672142/148412

share|improve this answer
Yeah, this seems to be more suitable, since I may have other options I could change, even the callback function might change. The example looks great :) – Roland May 10 '12 at 8:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.