Question

Sorry bad title. For the life of me I can't figure out a good way to do this. I've got a jquery object that is used to set a cookie. Throughout my code I use that cookie to gather information from the user as well as perform various other functions. The problem is in certain instances I don't want the cookie name to be 'user-profile' I want it to be 'admin-profile'. Is there a way to somehow pass in an option when I call userProfile() and change the cookie name? I want the 'admin-profile' cookie to basically do everything the 'user-profile' cookie does. I'm a bit of a newb so code samples work best for me.

;(function($){
    $.fn.userProfile = function(opts) {

        var userProfileCookie = {
            name: 'user-profile',
            options: {
                path: '/',
                expires: 365
            }
        };

        function userHeightCookie() {
            var userData = $.parseJSON($.cookie(userProfileCookie.name));
            return(userData);
        };

        function readHeightCookie(userInfo) {
            $.cookie(userProfileCookie.name, JSON.stringify(userInfo), userProfileCookie.options);
        };

        function removeProfileCookie() { $.cookie(userProfileCookie.name, null, userProfileCookie.options); }

        if($(".slider").length > 0){
            $.cookie(userProfileCookie.name);
        }
    }})(jQuery);

$(document).ready(function () { $('#mastHead').userProfile(); });
Was it helpful?

Solution

Use the opts parameter for this:

;(function($){
    $.fn.userProfile = function(opts) {

        var name = (opts.name || 'user') + '-profile';
        var userProfileCookie = {
            name: name,
            options: {
                path: '/',
                expires: 365
            }
        };

        function userHeightCookie() {
            var userData = $.parseJSON($.cookie(userProfileCookie.name));
            return(userData);
        };

        function readHeightCookie(userInfo) {
            $.cookie(userProfileCookie.name, JSON.stringify(userInfo), userProfileCookie.options);
        };

        function removeProfileCookie() { $.cookie(userProfileCookie.name, null, userProfileCookie.options); }

        if($(".slider").length > 0){
            $.cookie(userProfileCookie.name);
        }
    }})(jQuery);

$(document).ready(function () { $('#mastHead').userProfile({ name: 'admin' }); });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top