Namespace Problem (I think)
Hi,
I've got a problem wich is based on namespaces conventions, I think.
Here is a function from inside the $(document).ready():
- var onGetGroups = function( response ) {
- userGroups = response;
- console.log( userGroups.data.length );
- for( var i = 0; i < userGroups.data.length; i++ ) {
- console.log( userGroups.data[i].name );
- $( '#allGroups' ).append( '<li class="ui-selectable" id="' + userGroups.data[i].id + '">' + userGroups.data[i].name + '</li>' );
- $( '#allGroups' ).selectable( { filter: "li", selected: function( event, ui ) {
- $( ".ui-selected", this ).each( function() {
- var index = $( '#allGroups li' ).attr( 'id' );
- var name = $( '#allGroups li' ).textContent;
- sg.push({id: index, name:name}); // PROBLEM!!
- } );
- } } );
- }
- console.log( sg );
- };
You can see my problem on line 12. This sg object is defined directly after the ready state but I can't change it from this position in code. I always get an error: "Uncaught TypeError: Cannot read property 'push' of undefined", wich is, as I think, a problem of namespaces.
- $( document ).ready( function() {
- var accessToken, uid, userGroups, sg = {};
I think the sg var is global and can get accessed from everywhere in the code. But it isn't. Where is my fault?
- Thx, Michael