2

First, let me just mention that this is my first attempt at a from-the-ground-up jQuery plugin.

For starters, I want my plugin to accept user defined options (+parms) via an object. For example:

<script>
    $(document).ready(function() {
        $('.myClass').augaetrk({
            myobja : {zero:'obj00 zero',one:'obj01 one',two:'obj02 two'},
            myobjb : {zero:'obj10 aaa',one:'obj11 bbb'}
        });
    }); 
</script>

Note: The names of those objects will not be predefined. They will be at the discretion of the developer using the plugin.

To cut a long story short, within the plugin I parse (for example) a link's CSS classes to identify which user parms object I should be using further into the plugin. For example.

<a class="other-class-1 this-myobja otherclass-2" href="http://mylink.com">some link</a>

I can get the class - in this case this-myobja and the name of the object I need, myobja. What I can't seem figure out is how to use that string/value as the name of the object whose parm values I want to access/utilize.

At the moment, I'm each'ing through the options and using a simple if to get to the object I want. It works but I would think I'm adding a lot of unnecessary overhead. In PHP I would use a $$ but I can't seem to find that equivalent in jQuery.

How can I take the value I'm getting from parsing the link's class= and then use the value to get corresponding (user) options?

If you're able to help please type slow and assume I know even less than you think I might know. While probably a slightly advanced programming concept, I'm by no means an advanced - or even average - jQuery developer. I'm sure you're busy but if you give an answer/example please keep in mind I'm trying to learn so a bit of explanation would be greatly appreciated. Thanks.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Chief Alchemist
  • 634
  • 2
  • 7
  • 19

3 Answers3

3

If you can get the name of the relevant part of your object from the class of the a, then you can reference it like this: object["stringKey"] or object[variable]. Given your example:

// this object mimics the object passed to your plugin
var myobj = {
    myobja: {
        zero: 'obj00 zero',
        one: 'obj01 one',
        two: 'obj02 two'
    },
    myobjb: {
        zero: 'obj10 aaa',
        one: 'obj11 bbb'
    }
}

var objectName = "myobja"; // This would be the value from the A elements' class
alert(myobj[objectName].zero)

Example fiddle

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Hi Rory. Thanks. I suspected it was a matter of my not quite having my head wrapped around proper jQuery notation. One additional question please - How do I deal with - or not - objectName and quotes. That is, when I parse the classes to get the object name I want, do I then put that in quotes? Append quotes? – Chief Alchemist Dec 08 '11 at 17:50
  • Got it sorted. Thanks Rory. Those last two lines were exactly what I needed. I feel stupid, but at the same time I Googled high and low looking for an example and found nuttin'. Cheers! – Chief Alchemist Dec 08 '11 at 20:00
0

You can use square braces to use strings as keys for objects.

var object = {};
var str = 'prop';
object[str] = 12;
console.log(object.prop); // 12
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Yes, but having them passed in as options was throwing me off. None the less, as I mentioned, my problem is probably one of notation and I think this might help too. – Chief Alchemist Dec 08 '11 at 17:55
0

Welcome!

You're user options you'd like to receive are the objects form the above source? myobja, myobjb? So what about storing these objects in an associative array?

objects["myobja"] = myobja;
objects["myobjb"] = myobjb;

Is that somewhat helpful?

nuala
  • 2,681
  • 4
  • 30
  • 50
  • That's possible, I suppose. However, my bottleneck isn't how I store the parameter but how I access them as based on a value from further down the process. – Chief Alchemist Dec 08 '11 at 17:54
  • yeah you said start from the basics but I assumed if you know how to create an a. array you know how to pull the data back :) I see Roy edited his answer accordingly :) sorry for this! – nuala Dec 08 '11 at 20:23