Hello guys. I haven't posted in a while but I ran into something interesting I thought I would share.
I have been working on some side projects messing around with mobile safari and I ran into a peculiar problem. I was binding the touchstart, touchmove and touchend events to a method and attempting to get the pageX and pageY from the targetTouches object but I kept getting undefined. For some reason targetTouches did not exist despite the fact that apple documented that it should exist in mobile safari events. As far as I'm aware there is no way to reproduce this on the desktop since there are no touch events and I have not seen a tool that matches firebug on the iPad this was a difficult issue to track down.
Well to make a long story slightly less long as it turns out the culprit was my good buddy jQuery. Despite my reading and re-reading the jQuery docs I could not find anywhere where this is documented but it looks like when I used jQuery to bind those events jQuery took the resulting event and created a w3c compliant event object and stuck the original event in the new event object. This is no doubt necessary and useful for removing odd browser quirks but it would have been really helpful to document this behavior somewhere.
So if your using jQuery and you need to get at that original event in all its mobile safari touchy goodness you will probably want to give the event object jQuery provides you one of these:
e = e.originalEvent;
I haven't seen much documentation on this so I thought I would talk a little about what I have learned about javascript scope. So say you want to put all your methods into a main class and have sub classes and methods several layers deep. Everything would be so clean and organized but how the heck do you do that in javascript? Unfortunatly javascript does this very different from say ruby or java.
In javascript if you wanted to do something like google.map.Refresh()
first you need to make your main object like this.
google = {}
then make your sub object with a constructor like this.
google.map = function() {
}
then you can add your class methods to your map object like this.
google.map.prototype.Refresh = function() {
//map refresh code
}
if you decided to make google.foo.map.Refresh() all you do is add this in below your main object.
google.foo = {}
google.foo.map = function() {
}
google.foo.map.prototype.Refresh() = function() {
}
It is important to note that you can only add methods to objects with constructors so google.foo can't have a method but google.foo.maps can have the Map method since it was made with a constructor. I hope this helps clarify things while you organize your javascript.
So what I am trying to do is run build on an object when it is first created (ex. User.new) then set some default values. First I was doing this in the controller:
u = User.new
u.address.build(zip => foo, :state => bar)
BUT
As I understand it though his is the type of thing that should go in the model. I thought I had a solution for this with:
def after_initialize
address.build(:zip => foo, :state => bar)
end
But after a while I started to notice problems. As it turns out after_initialize while seeming to only run after initializeing the model object also runs after a find as well. Obviously this caused problems. So I'm back on the hunt for a new solution.
As always I will post a solution if/when I find one.