How to set a variable using geolocation.getCurrentPosition - geolocation

This is related to this post, but different in that I am trying to understand why I cannot set an outside variable from inside of geolocation.getCurrentPosition
I can't seem to get any kind of return value from it, and I can't set a value from it. I can only access the position object from inside of it. I have tried passing in this.
I forked derek-朕會功夫's fiddle and modified it:
https://jsfiddle.net/TwoFistedJustice/c3qr29L7/1/
Here is another fiddle based on this post:
https://jsfiddle.net/TwoFistedJustice/yu8Lzjvx/4/
I'm very perplexed.
Could someone please help me to:
Understand what is going on behind the scenes to make it behave it
this way.
Understand how to set a variable in an outer scope from inside
getCurrentPosition().

I've been reading up on the call stack and callback queue. And I gather that the behavior that has been perplexing me has to do with how Javascript handles the various stacks and queues. This piece by Jake Archibald helps to clear it up, though it is bit much to absorb all at once.
If I add a setTimeout with any delay value, even 0, foo is USUALLY changed. With near-zero values, it varies a bit.
setTimeout(function(){
console.log('SetTimeout\n - Foo only changes if this goes AFTER the Promise\n foo:', foo)}
, 0);
This is a fiddle derived from the others that more clearly shows what is going on. https://jsfiddle.net/TwoFistedJustice/arkqb0x8/71/

Related

(Roblox scripting) help needed for "GetMaterialColor" or "SetMaterialColor"

I am new to roblox scripting, and I am working on a game. I am trying to make an exploration game with multiple planets. I want the colors on the surfaces of the planets to vary, but I also wish to use smooth terrain, as it is easier to use and looks nice. from reading a bit online, i have figured out i need to use "GetMaterialColor" or "SetMaterialColor". however, "SetMaterialColor", the one i needed specifically, requires two bits of information- the material and the color.
The issue comes from the "Material" part of this, as I have no idea how to make the script recognize what material I want to change. i tried multiple things, including but not limited to:
(grass, #,#,#)
(grass) (#,#,#)
("Grass"), (#,#,#)
("Grass", #,#,#)
or even just (#,#,#), without trying to get a specific material at all
so yeah, I need some help
here is the code:
local function onTouch(hit)
game.Workspace.Terrain:SetMaterialColor
end
script.Parent.Touched:connect(onTouch)
(there should be stuff after SetMaterialColor, that is what i need help with)
If you read the documentation on Terrain:SetMaterialColor(), you'll see that the first argument is a Material type, which is an Enum. So the method expects an Enum (or number to be more accurate), not a string denoting the material.
At the same time the second argument is a Color3, so (#,#,#) isn't apt, using it with the constructor Color3.fromRGB(#,#,#) is. If you are ever confused about what a method returns or expects, try referring to its documentation on https://developer.roblox.com/.
Here's an example of correct usage:
workspace.Terrain:SetMaterialColor(Enum.Material.Grass, Color3.fromRGB(123,123,123))
And ofcourse, Event:Connect() instead of Event:connect()

Unusual UITableView crash on -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array

I've searched a ton and haven't found an answer that seems to address the problem I'm having.
I have a UITableViewController. In the viewDidLoad method I load objects from a database and then call reloadData.
Now generally this works fine. However, now I'm implementing sorting, which is persisted so that the same sorting algorithm is used on the next app launch. Note in all cases, the size of the array backing the section is the same after sorting.
If I use the default sorting method, I get no crash, even though it runs through the same sort > call delegate > reloadTable code path. However, if I use an alternate sorting method, which uses the same code path, I see the table view ask for number of rows, and get the correct number, but then I get this exception. Only the objects are sorted in a different order, so this makes no sense to me.
Also of note is that if I sort the objects (which uses the same code path) after the table has been displayed, it works fine.
This makes no sense to me, and almost seems like a UITableView bug, but I hope it's something I can fix.
My app is open source, so I can link directly to the relevant files:
https://github.com/einsteinx2/iSub/blob/weird_crash/Classes/UI/Reusable/ItemViewController.swift
https://github.com/einsteinx2/iSub/blob/weird_crash/Classes/UI/Reusable/ItemViewModel.swift
The most relevant methods are ItemViewModel.sortAll() and ItemViewController.itemsChanged(viewModel: ItemViewModel)
I've run out of ideas on how to debug this. It just makes no sense.
Some trial and error things I've tried:
Using only one table section
Calling reloadData after a delay to rule out some kind of race condition
Not calling reloadData in my itemsChanged delegate method (still crashes, as reloadData seems to be called automatically by the table view controller)
Nothing seems to make any difference. I'm completely confused how this could be possible. If anyone has any ideas on how I can continue to debug this, or how it may be possible, I'm all ears. Thanks.
EDIT: To make matters more confusing, I have similarly persisted sorting for albums, which use the same ItemViewController, however returning to an album that was previously sorted does not cause a crash, even though it should be doing the exact same thing and running the same code path.
Wow, I finally figured it out and talk about edge case!
Turns out the crash has to do with the table section indexes (the letters on the right hand side), not the actual data loading or sorting.
I was using the unicode bullet character • when not sorted by name. Somehow this worked when you sorted live, but there is some bug in UITableView that causes the internal array of section indexes to be empty when you load the table with that data. I switched to using the black circle character ● and now it no longer crashes.
I think this has to be the smallest change for the most amount of investigation work I've ever done in 8 years of coding for iOS.
Hope this helps some random person in the future. I'll report a bug to Apple, though likely it won't be high priority so who knows if/when they'll fix it.

Loading select options after setting value

My program flow requires that I first set the value of a select option in the Viewmodel and then I load the available options into the Viewmodel. This causes problems, typically the first available option will be seen in the selection list after this, while the selected option value stays at the specified value.
I was hoping that setting the new "valueAllowUnset" would help, but instead my page becomes unresponsive when using it.
Therefore I currently set the original value again after loading the options. Strangely, this only works when I set the value, set a different value and then set the value again, e.g.
self.data()[field](orgValue);
self.data()[field]({});
self.data()[field](orgValue);
(I store values for multiple selection lists in the self.data observable and access them with the property index "field".)
I tried to strip this down to a simple JSFiddle (http://jsfiddle.net/timvdh/HN6DE/), but the behavior does not show there, although the example pretty much resembles my actual application. However, I am using about 30 select lists there.
So I was hoping, that someone can make an educated guess about what goes wrong in my application, I am hoping that the fact that I have to set the original value three times maybe gives a clue.
EDIT: This fiddle demonstrates, that setting the selected option before and after filling the options list does work with "valueAllowUnset": http://jsfiddle.net/timvdh/HT5Tp/
I am using JQuery Mobile with knockout.js and that caused the problem (sorry I did not even think about JQM being the problem in the first place). I am now setting the option before filling the list and after filling the list I call
$("#selectmenuID").selectmenu("refresh");
to display the option.
The hanging browser was not caused by knockout, there are additional subscriptions to the selectmenus in my app. "valueAllowUnset" changed the behavior of the selectmenus and triggered loops caused by problems in my code.

Bind functions to elements inserted in the DOM? (Implicitly?)

This is probably jquery basics, but I can't find a solution after much googling.
How do you attach "non-events" to elements inserted in the DOM?
For events, like click, we can use live() or bind().
How would you, for instance, initialize tabs() or addClass() to a new element?
Specifically, I'm trying to get tabs() to work in the content of an ajax loaded dialog, but I think the correct solution should be applicable to any situation.
I did see a trick that involved $('body').mousemove() which did work, but that is still binding to an event and obviously a hack.
For instance, how would you get addClass() to fire on a newly inserted table row?
I mean to do this implicitly, meaning that I don't want to write out specific instructions for every event that adds nodes to the dom, I just want it to "run in the background".
Let me know if this needs clarification, I see many similar questions on SO but no answers that have helped.
EDIT: Simple example: A page calls $('a').addClass('highlight') which works on all anchors in the page. A new anchor is then added to the page dynamically by jQuery, but does not get the class added.
EDIT: I have tried all kinds of bind(), trigger() and change() methods but I'm afraid I'm barking up the wrong tree.
you need to look at livequery it will allow you to apply things to newly added elements
also if your adding the element you can do
$('body')append('<div>some content</div>').tabs();
or something like that
I know that I may contradicting your "non-event" rule here, but just by saying that you want something "triggered", you're already implying some kind of event.
In that case, may I suggest jQuery custom events? You may want to create a custom event, then trigger it manually somewhere in your code. It's not automatic (like when you add a row, BOOM, it fires™), but that's the closest thing I can think of with what you were describing.
Specifically, you may want to look at jQuery's .bind() and .trigger() methods.

ASP.Net MVC - strange params caching behaviour in Actions

I'm facing a strange problem in my project. My actions are getting old param values instead of the actual values which are in Request.Params. I created a HomeController.Echo(string text) action to illustrate it (see screenshot). When I call the action for the first time like "Home/Echo?text=aaa" everything works fine. When I call the same action second time with different text value ("Home/Echo/text=bbb"), I get the old "aaa" value again in my action "text" parameter. Strange think is that Request.Params contains the right "bbb" value.
I'm thinking if there's something I could break myself, but can't figure out anything. I'm serving controllers from IoC container, I overrided ControllerActionInvoker.InvokeActionMethodWithFilters method (to inject dependencies into filters from IoC) and I'm handling HttpApplication.AuthenticateRequest. Im'not working with params/binding anyhow in any of these...
screenshot
The problem was caused by some threading issues probably - I forgot to register controllers in my IoC container with per-request lifecycle (they were registered as singletons).
Have you debugged through the application to see where the value is getting switched out. A simple watch on the text variable (whatever you call it in the code) should yield where the variable gets changed. Without code to run through, I have no clue where it is happening.
I would say write a test, but there is still a possibility of UI interfering here. If you find where in the code it is changing, then write a test to confirm the bug and start whacking at it.
I suggest you to start commenting all the methods you overrode until you isolate the problem. In worst case you will get to the point where the ASP.NET MVC wizard left your project when you created it and where parameter binding definitely worked.

Resources