I have a jQuery plugin that loads json data via an ajax call - the returned data is inserted into an existing object.
When I try and reference the newly inserted nodes I get an 'undefined' - though the data looks good in the console.
If I manually define the data then I can access it it fine - even though the structure is the same.
The data is loaded on the init event (there will be multiple function calls in the .then() but I'm just showing the initial call that is failing - each of these will reference the same _t.criteria.base.x where x is the inserted json ):
var _t = this;
_t.init = function() {
_s = $.extend({}, defaults, options);
$.when(
loadDefaultCriteria()
).then(
renderCommunities
);
}
This loads the data:
_t.criteria = {
base: {},
current: {},
picks:{}
};
var loadDefaultCriteria = function() {
$.getJSON
(
'/path/to/script.php',
{
action: 'loadDefaultCriteria',
})
.done( function( data ){
_t.criteria.base = data;
})
.fail( function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
});
}
this is the code that fails (debug logs still in place)
var renderCommunities = function(){
console.log(_t.criteria);
var c = $(_s.containers.communities);
var i = _t.criteria.base.communities;
console.log(i);
c.empty();
c.append('<ul>');
$.each( i, function( idx, _i ) {
console.log(_i);
});
}
the console.log(_t.criteria) outputs this (I'm only showing the expanded tree for the base object ):
Object {base: Object, current: Object, picks: Object}
base: Object
communities: Object
subscribed: Array[2]
0: Object
1: Object
unsubscribed: Array[1]
0: Object
current: Object
picks: Object
It doesn't matter which Object I try and reference below base they all return 'undefined', so in the code above console.log(_t.base.communities) will return undefined.
How do I insert a subset of JSON data/nodes into an existing object and then access them?