Inspired from tonights ATL.JS talk
Closure and the for loop problem: If you really like 10
for(i=0; i < 10; i++){
setTimeout( function(){
console.log("i is " + i);}, 10);
}
Javascript is A) Lazy and B) Single Threaded. So.... contrary to what you want: For loop executes in the thread, AND then the i inside the closure function executes and sees that i now = 10 (last value).
Scope is a matter of perspective: if you prefer undefined instead of 10
for(i=0; i < 10; i++){
setTimeout( function(i){
console.log("i is " + i);}, 10);
}
So now, i is an argument, and it's not assigned when the function is created. It stays undefined.
And even explicit assignment doesn't help. The y here is part of the for loop, so gets assigned 9 times, then function executes:
for(i=0; i < 10; i++){
var y = i;
setTimeout( function(){
console.log("i is " + y);}, 10);
}
So for readable, manageable scope: create a function to create the function, so that each execution of the loop the iterator value is captured to a isolated scope and preserved until later.
function showI(i){
function show() {
console.log("i is "+ i);
};
return show;
}
for(i=0; i < 10; i++){
setTimeout( showI(i), 10);
}
Monday, September 15, 2014
Thursday, July 4, 2013
AngularJS - Scope Kata
Scope is where a good part of Angularjs magic happens, binding allows data to be updated by events on the DOM and for changes of data to be reflected by data in the DOM. Scope is more than just a handle for a bunch of variables and it can behave unexpectedly until you understand what's going on behind the scenes
"Scope is not the model.. it is just a reference to what's happening [inside the dom]" egghead.io tutorial
- Root scope sits at the top of the scope inheritance, changes to this model
can be reflected across all other application scopes which are children of the root scope. - Root scope is accesible without direct reference
- Controller scopes are children of root scope, and variables not found local will be pulled from parent scopes all the way back to root.
- If you declare 2 of the same controller on a view, they are siblings and each has it's own scope, so variables are not shared between them
Plain Variables
Plain variables lose inheritance as soon as written in child scopes. They are local to the current scope only after that.Model Variable
Model variables are declared as properties of an object "model.variable". These will maintain inheritance from parent to child scopes, as unlike plain variables, each child is pulling the model reference from parent and only changing the property on the shared object.
Of course if Controllers are siblings, the scopes are not shared regardless if you use plain variables or model notation variables.
Friday, January 4, 2013
Java, Groovy, Android and the URL with underscore problem
lesson for today:
Groovy HTTPBuilder chokes on URLs where the hostname contains and underscore character. For example http://xx_host.name.domain.com. Apparently the blame lies with the underlying java URL class which fails to parse due to the "illegal" underscore character.
What I find weird is how quietly folks seem to be accepting this limitation. Affects android as well. Yes, strictly speaking underscores are not valid hostname characters, but apparently that rule has been relaxed for subdomains and DNS configurations? Either way from the various posts I've seen looks like underscores are here to stay and problems have been encountered with facebook, amazon aws and other big names.
If there's a workaround out there other than injecting the IP address instead of hostname would love to hear about it. I really, really hate it when my code is tied to a strict IP address.
Groovy HTTPBuilder chokes on URLs where the hostname contains and underscore character. For example http://xx_host.name.domain.com. Apparently the blame lies with the underlying java URL class which fails to parse due to the "illegal" underscore character.
What I find weird is how quietly folks seem to be accepting this limitation. Affects android as well. Yes, strictly speaking underscores are not valid hostname characters, but apparently that rule has been relaxed for subdomains and DNS configurations? Either way from the various posts I've seen looks like underscores are here to stay and problems have been encountered with facebook, amazon aws and other big names.
If there's a workaround out there other than injecting the IP address instead of hostname would love to hear about it. I really, really hate it when my code is tied to a strict IP address.
Friday, April 8, 2011
Rails - Record Handling Tricks learned the hard way
- use dynamic model query methods described in: http://api.rubyonrails.org/classes/ActiveRecord/Base.html
- Story.order("column_ord").find_all_by_state("B").size returns the collection
- Story.order("column_ord").find_by_state("B").size returns only one story
- render(@collection) throws error if said collection is empty, so check <% if @collection then render(@collection) end %>
- assigns[:variablename] is how to get hold of a page object in the controller_test
- Story.order("column_ord").find_all_by_state("B").size returns the collection
- Story.order("column_ord").find_by_state("B").size returns only one story
- render(@collection) throws error if said collection is empty, so check <% if @collection then render(@collection) end %>
- assigns[:variablename] is how to get hold of a page object in the controller_test
Ruby: Stringception - a string within a string
good reference on all the various ways to define strings in ruby
http://www.techotopia.com/index.php/Ruby_Strings_-_Creation_and_Basics
So that you can run testing like:
assert_difference( %[Story.count :conditions => 'state = "P"']) do
put :drop, :id => story.to_param, :state => 'P'
end
Where the query string contains a string.
http://www.techotopia.com/index.php/Ruby_Strings_-_Creation_and_Basics
So that you can run testing like:
assert_difference( %[Story.count :conditions => 'state = "P"']) do
put :drop, :id => story.to_param, :state => 'P'
end
Where the query string contains a string.
Thursday, April 7, 2011
jquery w/ rails : ajax update of drag & drop item
A script added to my html.erb file, at page load it establishes sortable columns, and ajax call to automatically update an item when it is dragged from one column to another:
The drop_url references as routes.rb entry /board/drop to a specific board_controller method. Upon retrospect, I'm thinking this ought to work with the default update method on my :story resource. Note to self, if your update method doesn't update... check that you haven't forgotten your parameter naming conventions. status != state no matter how you write the method to update.
$(function() {
$(".sortcol").sortable({
connectWith: ".sortcol",
receive: function(event, ui) {
$.ajax({
url: "<%= drop_url() %>",
type: 'PUT',
data: $.param({id : ui.item.attr('id'), state : ui.item.parent().attr('id')}),
success: function(d) { ui.item.effect("pulsate", {} , 500 ) }
})//end get
} //end receive function
}); //end sortable
}); // end function
The drop_url references as routes.rb entry /board/drop to a specific board_controller method. Upon retrospect, I'm thinking this ought to work with the default update method on my :story resource. Note to self, if your update method doesn't update... check that you haven't forgotten your parameter naming conventions. status != state no matter how you write the method to update.
Saturday, April 2, 2011
Rails -migrations with column default changes
So wanted to add a default value for a column, but also wanted to do a proper rollback method on the migration. My problem may have been more with SQLite than rails, something about it not liking alter column commands.
Anyway:
change_column :table, :column, :type, default = 'value' did update the schema.
But also needed to add the date
Table.update_all( "column = 'value'", "column IS NULL") to fix unset values.
Didn't find a good syntax to remove the default setting from the schema.
Anyway:
change_column :table, :column, :type, default = 'value' did update the schema.
But also needed to add the date
Table.update_all( "column = 'value'", "column IS NULL") to fix unset values.
Didn't find a good syntax to remove the default setting from the schema.
Subscribe to:
Posts (Atom)
