I have been attempting to organise my JavaScript/jQuery somewhat better, but am having trouble with objects.
Please take a look at this JSFiddle
The fundamental pieces are as follows:
(function($){
$.toast = function(params) {
return new toast(params)
}
var toast = (function(params){
var params = params;
var show = function() {
// getToast() returns jQuery DOM object
getToast().show();
}
// Public API
return {
show : show
}
});
}(jQuery));
$(document).ready(function(){
var toast1 = $.toast(params);
var toast2 = $.toast(params);
toast1.show();
toast2.show();
});
The trouble lies where I am calling a function on the object(s) created.
Within the $(document).ready() function, I instantiate 2 objects, which I then call show() on.
The show() method simply changes the display property.
Only one of the toasts shows, however.
I've looked into different ways of instantiating the objects to no avail.
I've also moved around the "new" keyword, also to no avail.
I started my journey to cleaner IS with object literals, but I had trouble binding events to the correct object. Done weird stuff was happening where the bound function was being applied to all objects.
I like the way I instantiate the toasts, so I'd like for the code within the $(document).ready() to stay the same, if possible.
Any/all help is appreciated as I probably need a jolt in the right direction.
Cheers.
return new toastwrapper is completely useless. Yourtoastfunction returns an object, so there's no reason to call it withnew. Just move all your code directly inside of$.toast.varbefore the function definition ofgetToast.