NewBie Question, jQuery: How can we implement if...else logic and call function

Posted by Rachel on Stack Overflow See other posts from Stack Overflow or by Rachel
Published on 2010-04-11T22:38:27Z Indexed on 2010/04/11 22:43 UTC
Read the original article Hit count: 303

Filed under:
|

I am new to jQuery and so don't mind this question if it sounds stupid but here is something that I am trying to do :

I have 3 functions like:

AddToCart Function which adds item to the shopping cart:
//offer_id is the offer which we are trying to add to cart. 
    addToCart: function(offer_id)
    {
        this.submit({action: 'add', 'offer_id': offer_id}, {'app_server_url': this.app_server_url});
    },


RemoveFromCart which removes data from the cart
//target is link clicked and event is the click event. 
    removeFromCart: function(target, event)
    {
        this.uniqueElmt('cart_table').find('.sb_item_remove').unbind('click');
        var offer_id = $(target).parent().find('.offer_id').html();
        this.submit({action: 'remove', 'offer_id': offer_id, 'next_action': this.config.current_action}, {'app_server_url': this.app_server_url});
    },

Get the current state of the cart   
//return string which represents current state of cart. 
    getCartItems: function()
    {
        return this.contents;
    }

Now I am trying to do 3 things:

  1. if there is no content in cart and addToCart is called than some action, so basically here we need to check the current state of cart and that is obtained by calling getCartItems and if it is Null and than if addToCart is called than we perform some action
  2. if there is content in the cart and addToCart is called than some action,so basically here we need to check the current state of cart and that is obtained by calling getCartItems and check if it is Null or not and than if addToCart is called than we perform some action if we had some content in the cart.
  3. if there is content in the cart and removeFromCart is called some action, so basically here we need to check the current state of cart and that is obtained by calling getCartItems and if it is not Null and if removeFromCart is called than we perform some action

Pseudocode of what I am trying to do:

    if there is no content in cart 
        and addToCart is called than 
        $(document).track(
                {
                    'module' : 'Omniture',
                    'event' : 'instant',
                    'args' :
                    {
                        'linkTrackVars' : 'products,events',
                        'linkTrackEvents' : 'scAdd,scOpen',
                        'linkType' : 'o',
                        'linkName' : 'Cart : First Product Added'  // could be blank, but can include event name as added feature
                    'svalues' :
                        {
                            'products' : ';OFFERID1[,;OFFERID2]',
                            'events' : 'scAdd,scOpen',
                        },
                    }
                    'defer' : '0'
                }
            );

    if there is content in the cart 
        and addToCart is called than
        $(document).track(
                {
                    'module' : 'Omniture',
                    'event' : 'instant',
                    'args' :
                    {
                        'linkTrackVars' : 'products,events',
                        'linkTrackEvents' : 'scAdd',
                        'linkType' : 'o',
                        'linkName' : 'Cart : Product Added'  // could be blank, but can include event name as added feature
                        'svalues' :
                        {
                            'products' : ';OFFERID1[,;OFFERID2]',
                            'events' : 'scAdd',
                        },
                    },
                    'defer' : '0'
                }
            );

    if there is content in the cart 
        and removeFromCart is called 
        $(document).track(
                {
                    'module' : 'Omniture',
                    'event' : 'instant',
                    'args' :
                    {
                        'linkTrackVars' : 'products,events',
                        'linkTrackEvents' : 'scRemove',
                        'linkType' : 'o',
                        'linkName' : 'Cart : Product Removed'  // could be blank, but can include event name as added feature
                        'svalues' :
                        {
                            'products' : ';OFFERID1[,;OFFERID2]',
                            'events' : 'scRemove',
                        },
                    }
                    'defer' : '0'
                }
            );

My basic concern is that am complete newbie to jQuery and JavaScript and so am not sure how can I implement if...else logic and how can I call a funtion using jQuery/JavaScript.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript