JavaScript Jquery dynamic OOP call - javascript

Ok, not sure if I am going to give myself any justice in trying to describe this. But..
I have an some code OOP style where you would call it like: objectName.functionName(properties, values, etc);
and as of the moment I seem to have reached a road block. I am multiple functions that do various things, but I have this one function that I want to act as a "Refresh" function for the many. Which would work if it was a single function I wanted to refresh from time to time. Overall i don't wish to reinvent the wheel either.
So my current click event looks like a call to the above. Where in the link I am using I have my hidden parameters per the needs of the functions which this works, fine But heres the catch 22 I recently ran into a need to have multiple refreshes on the same page for various functions. So I am wondering is there a way I can pass functionName as a variable to the object so it would work like
var funcNamVar = functionName;
objectName.[funcNamVar](properties, values, etc);
note the brackets [ and ] are just for representation of the concept I know they shouldnt be there

Sure. You can actually just do
objectName[funcNamVar](properties, values, etc);

Related

jQuery pass data from click event to doubleclick

Since I've tried and not succeeded many times I figure it's time to ask. I would like have several elements on the screen on which the user can click. Once the user decides to double click somewhere on the body I would like to console.log the text elements of the array.
This isn't nearly as intuitive as I thought it was going to be. The following example isn't all that practical, just curious why I can't get it to work.
EDIT: I would like to NOT use a global variable.
Fiddle can be found here
You just need to alter the scope of the array textArray so that your function ferryArray() can access it and loop over.
Here is the working fiddle.

jQuery create a callback thats not a callback?

Alright lets see if I can describe this.
I have a handful of functionality that was created sometime ago, and works swimmingly. However there is a new desired spec, so without having to rewrite the code base in a matter of speaking, and without having to double up on code to pull the same effect off I am trying to figure out how I can go about making something jump back higher in the code within the same function to repeat the run of the function rather then doing the same code again below.
What I have is a click based triggers ie:
$('.selector').click(function(){});
In this function is about 30 lines of functionality to create a new element and populate it accordingly. However unfortunately in that same bit of functionality there is conditions to wether it should or not.*The previous requirement was when the element it creates is open and populated just throw an alert() saying essentially wrap up what your doing, and then go on to the next. *Now the new requirement is just close that and open a new element. Which I've gotten to close out the existing, and do everything I want it to do, except the population of the new element which is above where the condition is currently. Knowing there is no "go to" type of logic in javascript (or last I knew). the only thing I can think of is taking the same code from above and putting it in the condition as well, doubling up on the code and having litterally 2 copies of the same bit. I want to avoid that, but cant think of a way to do it. So here I am looking for ideas
Knowing there is no "go to" type of logic in javascript (or last I
knew). the only thing I can think of is taking the same code from
above and putting it in the condition as well, doubling up on the code
and having litterally 2 copies of the same bit. I want to avoid that,
but cant think of a way to do it. So here I am looking for ideas
Why don't you just pull this piece of code out into a function? You can run the function if the conditional is true in the original instance, and run it all the time in your callback? This is fairly minimal refactoring, just move the code out of the logic into a separate function, keeping it as is and maybe making some of the referenced variables into parameters.
So something like this if you want to run all the actions regardless of the conditional statements:
...
if(condition){
actionA();
}
if(condition2){
actionB();
}
...
$('.selector').click(function(){
actionA();
actionB();
});
You're familiar with that pattern, right?
var aCallback = function(){........};
$('.selector').click(aCallback);

Javascript FSM Possibilities / Idea

So I have been making stuff in Unity3D and decided to try an extension called Playmaker. Basically is uses a FSM (Finite State Machine) to design the flow of states and events. You can drag an event to a different state to trigger another state of events, etc. (Reference : http://www.hutonggames.com/features.html)
NOTE : The actual product I linked has nothing to do with the idea I want to try and build. Just a reference.
Well I would love to be able to do something simliar in Javascript. I think I have some of the logic down but I'm thinking more about User Experience. I want a user to be able to create an FSM with my logic in the browser using Javascript.
I'm not asking for anyone to code this for me or anything as I am experienced enough in javascript to do the bulk of it. I was thinking more about the way you can drag one event to another and it creates a visual arrow showing the user what events are connected. If you look at the first tutorial on the referenced link I provided you will understand what I mean. The arrow length and curves would be dynamic. Possibly be able to drag around states to re organize the layout of the states. This would obviously change the way the arrows pointed as well.
I hope that all made sense.
Ideas? Pointers? Maybe someone has done something like this already? I did find one Javascript State Machine but it generates once, doesn't allow users to move anything, the event dragging to another state is very important.

Is this normal for AngularJs filtering

I'm pretty much new to angular, but I feel like this is kind of crazy.
I've got multiple collections being displayed via ng-repeat in one controller scope. There's an input field for each list to perform a simple query. I was playing around with various filters in my code and I ended up putting a console.log in my filter function. I realized that every time my filter function was called for one list, it was being called for all of the lists in the scope. Furthermore, it was calling the filter function twice each time. So with 3 collections, filtering one of the lists would call the filter function 6 times.
I thought maybe it was just my custom filter, so I tried it out on the default filter function. Same story. Here's my code:
https://dl.dropbox.com/u/905197/angular-filter-test.html
Go to the console and see for yourself :/
What am I doing wrong here? This seems like such a simple thing but it's doing so much work.
This is normal, angularjs uses a 'dirty-check' approach, so it needs to call all the filters to see if any changes exist. After this it detects that you have a change on one variable (the one that you typed) and then it re-executes all filters again to detect if it has other changes.
See the first answer of this question

Box2D: Setting more than one b2ContactListener to world? - Getting weird behavior

So I am very new to Box2D and I'm trying to figure out how to use b2ContactListener.
Are you allowed to set more than one Contact Listeners to a world? I would think so. But when set two contact listeners like so:
world.SetContactListener(listener);
world.SetContactListener(listener2);
It behaves like listener2 was the only one set. Why is that?
Also when I change the order of how I set the listeners like so:
world.SetContactListener(listener2);
world.SetContactListener(listener);
Then it behaves like listener was the only contact listener set and ignores listener2.
The only reason I think why this could be behaving like this is because both of the listeners override the BeginContact and EndContact methods so it's confused.
I am using the JavaScript port of Box2D (Box2DWeb) by the way. But if you know the solution to the issue in Objective-C or C++ that's fine as I know those languages.
The hint is in the name - "Set" rather than "Add". It implies that there is only 1 listener supported. Update your listener class to dispatch to multiple methods if you need to.
It seems you are confused about what overriding methods entails. Having the same method overridden on multiple objects does not cause anything to get confused. The issue is that there is only one listener meant to be registered at once (because having a lot of listeners would slow things down - it will get called a lot)

Categories