Tuesday, April 23, 2013

Jasmine spying on jQuery selectors

Quick blog post about how to spy for jQuery selectors. Say I want to write something like the following:
var $element = $('div.my-element');
How would you write a Jasmine specification to drive this line of code in a Backbone.View function? Here it is:
it("find the element using a jQuery selector", function() {
   var spy = spyOn(jQuery.fn, 'find');
   this.view.doSomething();
   expect(spy).toHaveBeenCalledWith('div.my-element');
});
The reason you can do this is that $(selector, context) becomes $(context).find(selector). By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $() function (from http://api.jquery.com/jQuery/#jQuery1). Had to write this down in a blog so I remember it again some day. Cheers!

Wednesday, February 20, 2013

Disabling hashListening in jQuery Mobile 1.2

Just a quick note. Spending a little time today integrating Backbone.js with jQuery Mobile. Both frameworks have routing solutions which do not work together. However, there is some simple configuration that you can use to turn off the jQuery Mobile routing in favor of Backbone.js routing:
$(document).bind("mobileinit", function () {
    $.mobile.ajaxEnabled = false;
    $.mobile.linkBindingEnabled = false;
    $.mobile.hashListeningEnabled = false;
    $.mobile.pushStateEnabled = false;
});
The trick to getting this to work is loading this bit of code before you load jQuery Mobile. More information at http://jquerymobile.com/demos/1.2.0/docs/api/globalconfig.html.

Friday, February 08, 2013

Disappearing USB ports on Mac Pro (Early 2009)

Just a quick note before bed. Recently I've been having issues with my Mac Pro not recognizing my HiFiMan headphone amp through a USB connection. I thought it might be the amp, but I tried it on my MacBook Pro and it recognized it immediately. Tonight, I noticed that my iPod would not connect iTunes on my Mac Pro. Searching around, I found an easy solution:

  1. Shut down your computer.
  2. Unplug the computer from power and wait about 15 seconds or so.
  3. Plug the computer power cord back into a power outlet.
  4. Restart your computer.


Voila! I have my USB ports back and my system is recognizing both the HiFiMan amp and my iPod.

Thursday, February 07, 2013

Effective use of the Rails has_and_belongs_to_many association

Just a note to myself and others using the Rails has_and_belongs_to_many association:
  • The naming of the association table is by alphabetical convention. For example, a many-to-many relationship between Assemblies and Parts models would result in an association table named assemblies_parts. The migration will look like:
    class CreateAssembliesPartsAssociationTable < ActiveRecord::Migration
    
        def self.up
            create_table :assemblies_parts, :id => false do |t|
                t.integer :assembly_id, :null => false
                t.integer :part_id, :null => false
            end
    
            add_foreign_key(:assemblies_parts, :assemblies)
            add_foreign_key(:assemblies_parts, :parts)
        end
    
        def self.down
            drop_table :assemblies_parts
        end
    
    end
    		
    Note that I am using the foreigner gem to implement foreign keys in my migrations.
  • Use this association type when you want a direct many-to-many mapping of models without any intervening association model.
  • The model mapping looks like the following:
    class Assembly < ActiveRecord::Base
    
        has_and_belongs_to_many :parts
    end
    
    and
    class Part < ActiveRecord::Base
    
        has_and_belongs_to_many :assemblies
    end
    
I just got caught by the alphabetically ordering of the association table name. Thought I would write something up about it.

Thursday, December 13, 2012

Pinning Rails 3.2 to a specific time zone

I've been wrestling with some issues of dates and times in Rails. Being that I'm still pretty new to Rails, I been soaking up knowledge about this platform and today was no different. ActiveRecord, by default, converts timestamps and dates to UTC and interacts with them that way. UTC is also the format that the values will be stored in the database. If this is not what you want, you can pin your Rails system to a specific time zone by setting the following two configuration options in config/application.rb:
    config.time_zone = 'Central Time (US & Canada)'
    config.active_record.default_timezone = :local
Hope this helps others that discover that the default behavior of UTC is not what is desired. Official documentation on these configuration items can be found at http://guides.rubyonrails.org/configuring.html#rails-general-configuration and http://guides.rubyonrails.org/configuring.html#configuring-active-record.

Thursday, September 13, 2012

Resolving connectivity issues with Verizon 4G LTE JetPack 4510L MiFi and your Apple devices

I've been dealing with the fact that my iPhone 4S and my first generation iPad could not connect to my Verizon 4G LTE JetPack 4510L MiFi hotspot. They did connect to it when I got this Mifi hotspot back in September 2011, but now they seem to flake out and ultimately never connect to it. Not sure if it was a MiFi issue or a device issue. I was getting to point of just returning the hotspot but thought a last ditch effort to resolve the issue may do the trick. Luckily, it seems to. Here's what I did.
  1. Make sure the firmware on the hotspot is up-to-date. Update if not.
  2. Changed the radio protocol to 802.11n. It was set to 802.11g.
  3. Changed the network key on the hotspot.
  4. Forget the network on the Apple device.
  5. Connect to other network, selecting your MiFi hotspot network. When prompted for network key, enter the new network key.
  6. Done!

Tuesday, July 10, 2012

Adding HTTP headers to AJAX calls in Backbone.js

Just a quick note about setting custom header in the HTTP request that Backbone.js manages. This is super easy to do. In the example code below, I have a Backbone.Collection assigned to messagesCollection variable and I'm triggering a fetch on that collection: messagesCollection.fetch({ headers: { 'x-my-custom-header-1':'foobar', 'x-my-rest-api-version':'1.0' } });