Some ExtJS patterns I enjoy

It’s always nice to stumble upon patterns that make code both functional and beautiful at the same time. Among those, of late I have been abusing these patterns when developing ExtJS components.

For example, a pattern from ExtJS itself uses Ext.applyIf to both ensure an object literal exists and then to apply default values unless they’re already defined. Such a pattern can find widespread use in any project.

this.simpleConfig = Ext.applyIf(this.simpleConfig||{}, {
  width:500,
  height:250
});

Another I rather enjoy I first noticed in an Ext Direct implementation for Rails. The functions being applied are defined after the plugin on a function prototype specifically for that purpose. It’s nice and clean.

liaison.klass.plugin.OnData = Ext.extend(Object, {
	constructor:function(config) {
		Ext.apply(this, config);
	},
 
	init:function(p) {
		Ext.applyIf(p, liaison.klass.plugin.OnData.Methods.prototype);
		p.addEvents('data', 'update-data', 'create-data', 'delete-data');
		p.on('data', p.onData, p);
	}
});
 
liaison.klass.plugin.OnData.Methods = function() {};
liaison.klass.plugin.OnData.Methods.prototype = {
 
	onData:function(action, result) {
		this.fireEvent(action+"-data", this[action+"Record"](result));
	}
};

Another pattern from ExtJS itself I hadn’t thought to mention, above, is concisely defining and calling functions based on some string. For example, I avoid the need for a case statement or an if block when deciding amongst functions prefixed with the string action. Instead, onData is a compound one liner that fires an event based on the results of a conditionally named function. Fun!

Finally, I noticed a short pattern for managing namespaces in a plugin by ExtJS forum member stephen.friedrich. I employ it when building up my various FormPanel classes.

(function() {
  var ns = Ext.ns('liaison.view.form.membership');
  ns.NewPanel = Ext.extend(liaison.components.form.DefaultFormPanel, {
  ...
  });
  Ext.reg('membership-new-form', ns.NewPanel);
)();

Simple, but effective. It’s nice to collect private field configuration items in local variables.

What patterns do you enjoy using?

Failing fast, Ruby style

While building up the server side RPC methods for client side calls from ExtJS’s new Direct RPC framework, I thought it would be useful to fail fast whenever the client caller violated the interface contract. Enter fail fast.

def submit(params)
  action = params.delete(:action)
  id = params.delete(:id)
  assert(action) {|v| ['update', 'create'].include?(v)}
...

Further up the stack, we can handle the exception and cleanly fail the client side request. From within active-direct.

    def invoke_method(model, method, parameters, tid)
...
      unless parameters.nil?
        return_val = model.constantize.send(method, *normalize_params_for(model,parameters))
      else
        return_val = model.constantize.send(method)
      end
      result['result'] = return_val.nil? ?  "" : return_val
 
    # Must catch descendant of Exception explicitly
    rescue FailFast::AssertionFailureError => e
      Rails.logger.error result['type'] = 'exception'
      Rails.logger.error result['message'] = e.message
      Rails.logger.error result['where'] = e.backtrace.join("\n")
...
    ensure
      return result
    end

The entry point to a RPC is a good place to ensure contract validity.

Still no cramdown — Yeah, DCCC still wants your money!

I get spam:

Jason –

This afternoon, the House passed a bill 223-to-202 tightening financial regulations and ensuring we put Main Street first.

For eight long years, President Bush and his Republican allies ignored growing risks in the financial markets. The Republicans failed to regulate financial markets leaving the big banks to take huge risks with our money and leaving taxpayers with the bill.

Today, Democrats took bold action and voted to protect Main Street’s families and small businesses — just days after Republicans were huddling with special interest groups in the Capitol over the strategy to kill financial reform.

But, unfortunately we know what that means - fat cat special interests will be gunning for Democrats who stood with us. The U.S. Chamber of Commerce has already pledged over $2 million in advertising.

That’s why this week we asked you to help us raise money for our Main Street Democratic Fund, and I am proud to announce you helped us to shatter our goal of raising $50,000 this week. We can’t thank you enough.
http://dccc.org/blog/archives/gop_masquerade/

We will keep you posted as we continue to fight for health care reform and help our candidates prepare for 2010. Thanks again for standing with us as we continue to stand up for you!

Best wishes,

Jon Vogel
DCCC Executive Director

P.S. For those of you who will celebrate tonight, the DCCC wishes you a very Happy Hanukkah!

Contribute Today:

But cramdown isn’t included.

The plight of homeowners has become a volatile political issue. On Friday, as the House passed a series of new financial regulations, it narrowly defeated a provision that would have allowed bankruptcy judges to modify the terms of mortgages. The measure was strongly opposed by the banking industry.

Maybe someone should ask Congresswoman Bean why that might be?

The Audit the Fed provision did make it into the house version of the bill. It’ll be interesting to see if such a provision survives both the Senate and the conference committee.

New Democrats delay, weaken reform, DCCC wants your money!

I get spam:

On Tuesday, Barney Frank helped us to launch our Main Street Democratic Fund to help Democrats who vote in favor of tough financial regulation.

Now, we’re ready to launch our next phase. Our ad team wants to produce hard-hitting new spots taking on Republicans who are voting with the lobbyists trying to kill reform. In order to do it, we’ll need to raise $25,000 more by MIDNIGHT TONIGHT to go on the offensive and run a full slate of advertising. Can you help us with a gift of $5, $10 or more today?

- Jon Vogel

Not so fast, though:

A group of Democrats friendly to Wall Street interests forced a delay in consideration of the landmark financial regulatory reform bill scheduled to hit the House floor on Wednesday, Financial Services Committee Chairman Barney Frank (D-Mass.) told reporters in the Speaker’s lobby.

Frank accused the New Democrat Coalition of blocking the bill because its members are being prodded by big banks to abolish the Consumer Financial Protection Agency and to allow major financial institutions to avoid state laws tougher than federal regulations.

Giving to the DCCC is no different than funding Blue Dog and New Democrats.

Fuck that.

No reform? No money for the DNC!

As usual, I agree with kos on this. The DNC can seriously bite me. I’m certainly not funding obstructionist, corporatist hacks.

Really? All we have to do is send the DNC $5 and we get ponies? The same DNC that is enabling corporatist Democrats to water down and destroy any hope for health care reform? That DNC?

Beware the Class.new dragons

When using this neat snippet from Dr Nic for testing a plugin I use internally, I came across the behavior described by Avdi. The proposed solution works great!

A single line modification to Dr Nic’s class ensures it works with class_inheritable_accessor from ActiveSupport.

def create_class(class_name, superclass, &block)
	klass = Class.new superclass
	klass.instance_eval &block
	Object.const_set class_name, klass
end

Pelosi on behalf of DCCC asked me for money, I said NO!

Basically what kos said. I’m not funding Blue Dogs. Not interested.

So here’s the bottom line — skip any donations to the DCCC. Their first priority is incumbent retention, and they’re (necessarily) issue agnostic. They’ll be dumping millions into defending these seats. Instead, give to those elected officials who best reflect your values.

If you’re going to give money, give to a progressive (or whatever) candidate of your own choosing. Don’t let the DCCC fund Blue Dog Democrats on your behalf that then vote against the president’s signature reform issue.

Microsoft kills Office Accounting

When I last looked at accounting software, I was somewhat concerned about choosing a Microsoft product. It appears my concern was well founded. If I had decided to use Office Accounting, I’d be shitting bricks right now: It’s discontinued (via email):

Dear valued Microsoft® Office Accounting customer:

We are writing to let you know that we will no longer
distribute Microsoft Office Accounting after November 16, 2009.

A new, exciting way to poison drinking water!

New Way to Tap Gas May Expand Global Supplies:

“We know the shale is out there,” said Lars Erik Oino, a Statoil geologist working at Chesapeake headquarters here, as he rubbed hydrochloric acid on a shale sample to test its mineral makeup. “This could have a huge impact on the European energy situation.”

Dude, look for it somewhere else. Hydraulic fracturing is a great way to poison underground water sources. I can’t wait!

I want to join any class action against Nvidia

If such a thing happens, I want to be a participant. My Dell XPS 1530m which shipped with the faulty part, a G84 based 8400GS-M, is starting to show signs of failure. Dell is simply offering to trade me another broken part under warranty, as all G84 GPUs are faulty by design.

I want to be made whole. That means a working GPU. As no G84 parts work, I ought to get a working laptop from Dell or paid for by Nvidia. I honestly don’t care who.