A hard spin on a patch of loose dirt on an otherwise smooth section of uphill is really hard to recover from. Your back tire is still going to be in that same loose dirt that caused you to spin in the first place. You can cock back the pedals quickly, but it will probably spin again. The best way to deal with patches of loose dirt on climbs is to be proactive and do nice smooth pedal strokes, and keep a higher cadence to avoid spinning in the first place. That's true for both standing and sitting.
Locking out your knee and shifting all your weight to one pedal with every standing pedal stroke makes slipping a tire more likely, and harder to recover from. Since all your weight is locked out and all on one pedal, once your tire slips it is over. Since all your weight is on one locked out leg when you spin, momentum sends your body forward toward that pedal on that side of the bike, and you can't get your weight back on the other pedal to do anything.
Try to drop your hips and keep a more even weight between the pedals, keep a bit of knee bend, keep the cadence up, and keep pedaling through the top and bottom of the pedal stroke (round vs. square pedaling). You will lose traction less often, and when you lose traction, it will be a less dramatic loss of traction. Instead of a big hard spin that throws all your weight forward, it will be a smaller slip. Because your weight is centered/balanced and the cadence is higher, you will already be on the next pedal stroke and already recovering. No dramatic hard spin where you end up with your nuts headed straight towards your top tube.