let say I want to do this:
var dashboard = {};
var page = "index";
$('.check').click(function(){
$(this).toggleClass("active").siblings().slideToggle('slow', function() {
dashboard['pages'][page][$(this).closest('li').attr("id")]['show'] = $(this).is(":hidden") ? 'collapsed' : 'expanded';
});
}
I get an error saying:
Dashboard.pages is undefined
Is there away to dynamically add pages and the children that follow without having to do the work of checking to see if it is defined first then if it’s not doing:
dashboard['pages'] = {};
because sometimes they may already exist and I don’t want to have to inspect the tree first I just want to build the branches as needed
EDIT
I changed pagename to page to show that page names will change and also I want to point out that the pages could really be anything too.
The idea is that you have any object that can contain objects with parameters without checking to see if the branches exist
It looks like $extend as stated will be the way to go just not sure how that works. Got to get my head around that.
Define get and set methods on an
Object. Actually it could be defined just on thedashboardobject and only its descendants, but that’s easy to do.Iterate through nested properties using this
get()method and callset()whenever a value has to be set.You could also extend/modify the
getmethod to accept the nested properties as individual arguments or an array or a string. Using that, you’d only have to call get once:Update:
Since the get method generically returns an object and does not know whether you need an array or some other type of object, so you would have to specify that yourselves:
Then you could push items to the settings array as
To actually have the get function construct the object hierarchy from a given string such as pages.user, you would have to split the string into parts and check if each nested object exists. Here is a modified version of
getthat does just that: