1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with inventory class using jquery

Discussion in 'jQuery' started by Jeremy Benson, Sep 3, 2015.

  1. #1
    Hey,

    I'm working on a class that's giving me a ton of problems. Somewhere in add_item my xml tags are getting stripped. One thing I do know is when I put an alert at the first of the function my first call to add_item returns blank text() for the xml object. It shouldn't though, because some started xml is set when I call set_xml.

    Can someone see why my tags are getting stripped, tell me what I'm doing that I shouldn't, and maybe help me out here. Thanks for the time.

    <!DOCTYPE html>
    <html>
        <head>
            <script type="text/JavaScript" src="jquery.js"></script>
        </head>
        <body>
            <script type="text/JavaScript">
              // Find out how to use callback inside each.
                function inventoryXML()
                {
               
                    var xml;
               
                    this.set_xml = function(xmlSet)
                    {
                   
                        this.xml = xmlSet;
                   
                    }
                   
                    this.add_item = function(nameSet, typeSet, amountSet, descriptionSet)
                    {
                        // Remember to find node where item exists and add to it, if not add it.                
                       
                            var xmlDoc = $.parseXML(this.xml);
                            //var $xml = $('<dummy />').append(this.xml);
                            //$xml = $('<dummy />').append(this.xml);
                            $xml = $( xmlDoc )
                            alert($xml.text());
                       
                        // there are no element so add the item
                        if(this.xml == "<?xml version=\"1.0\" encoding=\"utf-8\"?><inventory><items><\/items><\/inventory>")
                        {
                                
                            $xml.find('inventory').children().each(function(){
                           
                                $xml.find('items').text('<item><name>' + nameSet + '<\/name><type>'+typeSet+'<\/type><amount>' + amountSet + '<\/amount><description>' + descriptionSet + '<\/description><\/item>');
                               
                            });
    
                        }else{
                          
                            // there are items, check and see if the item exists
                           
                            $xml.find('items').children().each(function(){
                                   if(parseInt($xml.find('amount').text()) >= 1)
                                {
                                    // add one to the number of items
                                   
                                    $xml.find('amount').text(parseInt($($xml).find('amount').text()) + parseInt(amountSet));
                                    alert("if amount greater than or equal to one: " + $xml.text());
                                }
                           
                            // end loop through items children
                            });
                               
                           
                        }
                       
                         this.xml = $xml.text();                    
                         $('#test').html(this.xml);
                       
                        // end of add item function
                    }
                   
                    this.remove_item = function(itemSet, amountSet)
                    {
                                    
                       // find node if exists, if so get value. If value is one remove the node, if value is less subtract 1.
                       var $xml = $('<dummy />').append(this.xml);
                       var couldntRemove = false;
                      
                       $xml.find('items').children().each(function(){
    
                            if($xml.find('name').text() === itemSet)
                            {
                                // If the amount is greater than the amount to be removed
                                if(parseInt($xml.find('amount').text()) > parseInt(amountSet))
                                {
                                  
                                    $xml.find('amount').text(parseInt($xml.find('amount').text())- 1);
                                     
                                  // if amount is the same remove element
                                }else if(parseInt($xml.find('amount').text()) == parseInt(amountSet)){
                                    // There is only one left so remove the element.
                                       
                                    $($xml).find('item').text("");
                               
                                // If amount is more remove nothing
                                }else if(parseInt($xml.find('amount').text()) < parseInt(amountSet)){
                           
                                    // There isn't enough of the item to remove one.
                                    couldntRemove = true;
                                }
                               
                                // end of if name = itemSet
                            }
                       
                         // end of each
                       });
                   
                         this.xml = $xml.text();
                         $('#test').html("");
                         $('#test').html(this.xml);
                         alert($xml.text());
                        // end remove item function
                    }
                   
                    // end of class
                }
               
                var thing = new inventoryXML();
                thing.set_xml('<?xml version="1.0" encoding="utf-8"?><inventory><items><\/items><\/inventory>');
               
            </script>
            <div id="test" style=""></div>
            <br/>
            <button onclick="thing.add_item('dagger', 'weapon', 2, 'This is a really cool dagger.')">Add Item</button>
            <button onclick="thing.remove_item('dagger', 2)">Remove Item</button>
        </body>
    </html>
    Code (JavaScript):
     
    Jeremy Benson, Sep 3, 2015 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Dunno if you got this sorted (I don't poke my head into JQ all that often since all I'll end up doing is ripping jQ's mouth-breathing idiocy to shreds) but since nobody else has replied...

    If I were working with it as something that is being shown on page as HTML, I would NOT be using XML in the background except for data transfer. When you load, translate the XML into semantic markup, when you add, do it on the markup, and when you want to change/save turn the values back into a new XML document.

    As always, I'd also swing an axe at the jQuery-tardery since it's taking something simple and making it hard -- especially given how often you seem to be calling the parser for no good reason. The normal JavaScript DOMParser (and it's ActiveX equivalent) is WAY more versatile and would let you leverage the existing functionality of DOM manipulation in the XML.

    Though again, this REALLY feels like a case of doing things client-side that have no business being done client-side, or if they were done client-side the scripting should be assisting with a scripting off fallback on a normal everyday form.
     
    deathshadow, Sep 17, 2015 IP