javascript - JS make function available after object has been instantiated -


i'm building js framework using facade pattern , i'm having scoping issue. in html side when execute multiple methods. second method fails because first hasn't completed. i've tried jquery deferred no luck, , i'm thinking may have modify pattern of js accomplish this, but possible make function available after object has been instantiated?

my guess changing pattern module, i'm unsure , before going down millionth rabbit hole seems wise ask.... thanks

<script type="text/javascript" src="">    foo.dofirst('#id');    foo.availablesecond('#hooray'); </script> 

edit:

foo.dofirst(); calls series of functions nested within 'foo'. importantly objects created inside. foo.layers; array of objects

foo.dosecond(); needs array of objects created in foo.dofirst(); parse through , execute function.

to note. if execute first method alone, application loads, after page load can call second method , executes cleanly. makes me think need force second method wait until first finished.

second edit: ** below deferred attempt. **goal: 'jen.inflayers' defined before executing

third edit: foo.dofirst() = jen.dofirst(); jen object instantiated , jen.inflayers instantiated array, after instantiation objects pushed it. needs occur before function below called jen.dosecond(), loop , new object app load if titles match.

function addlayer(opt) { $.when(jen.inflayers).done(function() {   (var = 0; < jen.inflayers.length; i++) {     if (jen.inflayers[i].title === opt) {       setlayer({         url: jen.inflayers[i].url,         title: jen.inflayers[i].title,         visible: jen.inflayers[i].visible,         img: jen.inflayers[i].img,         lyrtype: jen.inflayers[i].lyrtype       });     }   } }); 

}

you can in many ways. deferred can help. decoupling functions way, here deferred solution:

foo = (function () {     var first_deferred = $.deferred();     return {         dofirst: function () {             console.log("first");             first_deferred.resolve();         },         availablesecond: function () {             first_deferred.done(function () {                 console.log("second");             });         }     }; }()); 

now if call:

foo.availablesecond(); // prints nothing foo.dofirst(); /// prints first second 

other order:

foo.dofirst(); /// prints first foo.availablesecond(); // prints second 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -