Lazy loading asynchronous function with jQuery Deferreds
Posted by Avi in Programming on May 12th, 2011
We are all well aware that out of process calls are very expensive, and a very common pattern, known as memoization, is used to “cache” the results, so a later call to the function returns a local result.
What do we do if that out of process call is asynchronous? For example an ajax request from a JavaScript application to a web service.
The problem with is that the first call to the function will fire off an asynchronous request.
console.log(a);
here the call to console.log may or not be occur before the function is ready. We must provide a callback to alleviate this situation:
console.log(thing);
});
But the second call to this function is not asynchronous, and does not require this awkward callback. Additionally, we all have experienced the “callback” soup which comes about when we have to wait for a series of asynchronous calls to finish, before our “grand finale”.
Let’s see how to deal with all this. Read the rest of this entry »
Bootstrapping ExpressionEngine to integrate with other scripts
Posted by Avi in Programming on February 15th, 2011
Very often I’ve had to have external scripts access the ExpressionEngine environment, including the database, the sessions, and whatever else. For example, a cron job, or just simple one off scripts. So there are two ways most people do this, both are annoying. The simplest, is to create a template with PHP enabled. This is annoying because of all the extra steps involved, plus you get the overhead of extra database queries. The second is to create a module action. This is even more annoying, less database overhead, but you have to go through extra loops of creating a module and installing it. Plus you get to work with meaningless URL like http://mysite.com?ACT=324.
For EE1, I had distilled out all the code necessary to instantiate all the global EE objects. This was a one time annoyance, and I had to, unfortunately, duplicate a lot (read:all) the code, but it has come in handy many times.
Recently, I had to do this for EE2 as well, and it was not so straightforward.
Of course I began by distilling index.php and CodeIgniter.php, duplicating all that code. The only tricky parts, are that you need a fake “controller”.
I handled this by creating a small class, instantiating it:
class A extends Controller {}
$EE = new A;
Once I got to that point, all I had to do was call:
$EE->core->_initialize_core();
Now you can integrate EE anywhere, including any other systems or frameworks:
$system_path = '/path/to/system';
include 'boostrap-ee2.php';
And we have our $EE super global god object.
The full code is hosted on Github:
Just had my first taste of India….
Posted by Avi in Programming on June 3rd, 2010
I got to turn this:
<?php query_posts('category_name=Projects2&showposts=3&orderby=rand'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php if($i==1)
{?>
<div class="set set1">
<div class="title"><h4><a href="<?php the_permalink();?>"><?php the_title();?></a></h4></div>
<div class="content">
<a href="#">
<?php $values = get_post_custom_values("image"); ?>
<img src="<?php echo $values[0]; ?>" width="242" height="148" alt="">
</a>
<!--content end--></div>
<!--set set1 end--></div>
<?php }
elseif($i==2)
{?>
<div class="set set1">
<div class="title"><h4><a href="<?php the_permalink();?>"><?php the_title();?></a></h4></div>
<div class="content">
<a href="#">
<?php $values = get_post_custom_values("image"); ?>
<img src="<?php echo $values[0]; ?>" width="242" height="148" alt="">
</a>
<!--content end--></div>
<!--set set1 end--></div>
<?php }
elseif($i==3)
{?>
<div class="set set1">
<div class="title"><h4><a href="<?php the_permalink();?>"><?php the_title();?></a></h4></div>
<div class="content">
<a href="#">
<?php $values = get_post_custom_values("image"); ?>
<img src="<?php echo $values[0]; ?>" width="242" height="148" alt="">
</a>
<!--content end--></div>
<!--set set1 end--></div>
<?php }
else
{?>
<div class="set set1">
<div class="title"><h4><a href="<?php the_permalink();?>"><?php the_title();?></a></h4></div>
<div class="content">
<a href="#">
<?php $values = get_post_custom_values("image"); ?>
<img src="<?php echo $values[0]; ?>" width="242" height="148" alt="">
</a>
<!--content end--></div>
<!--set set1 end--></div>
<?php } ?>
<?php $i++; ?>
<?php endwhile; ?>
<?php wp_reset_query();?>
Into this:
<?php while (have_posts()) : the_post(); ?>
<div class="set set1">
<div class="title"><h4><a href="<?php the_permalink();?>"><?php the_title();?></a></h4></div>
<div class="content">
<a href="#">
<?php $values = get_post_custom_values("image"); ?>
<img src="<?php echo $values[0]; ?>" width="242" height="148" alt="">
</a>
</div><!--content end-->
</div><!--set set1 end-->
<?php endwhile; ?>
<?php wp_reset_query();?>
Needless to say, my client will no longer be outsourcing to India.
Review of the Apostrophe CMS for symfony
Posted by Avi in Programming on April 21st, 2010
I know I gave my blessing before to diem, and I’ll still admit that it is a very good CMS. However, it has always bothered me a little bit just how invasive this CMS is. While it bills itself as a symfony plugin, it is in truth anything but. You must start out using diem, and then add a little symfony in when you need it.
I was reading, recently, a post on the sympal blog. In this post, Ryan Weaver echoed my latent feelings that a CMS should empower your site with CMS capabilities, but should not take it over completely. In other words, the perfect medium would be in the form of a plugin for symfony 1.x, or bundle perhaps for Symfony 2.x.
There was a comment put in there by somebody from the apostrophe team, which made me realize that, apostrophe was perhaps already meeting this goal. Seeing as I needed to pick a new CMS for an upcoming project, I thought I’d take it for a spin. Read the rest of this entry »
The end of the PHP vs Template language debate?
Posted by Avi in Programming on February 10th, 2010
For years there has been a ferocious debate about whether it is better to abstract away HTML creation with PHP by using a template language, or whether we should stick with PHP itself as the template language. I always thought the best way to go was a with a hybrid. A templating language which resembles XML, sort of like what you get with JSTL. There have been some attempts , which have been pretty good. Facebook, however, just took this one step further, and integrated XML into PHP with XHP. This is sort of reminicient of XML Literals in VB. They have released the code on github already. The best part about it is that every XML element is represented by a class, leaving room to create sophisticated widgets out of XML tags!
Diem, my new CMS of choice
Posted by Avi in Programming on January 4th, 2010
Every since I started getting frustrated with Expression Engine, I was always looking out for something different. A CMS aimed at developers. Just about every CMS on the market is aimed at letting less technical people get out their and create content. There are a few CMSs that bill themselves as a Content Management Framework. In other words, they provide with the tools to create your own custom CMS. In fact, I would actually put Expression Engine in that category. Others that come to mind are Drupal and EzPublish. Both of these, for some reason, I was never able to wrap my head around. Then again, I never spent much time with them either. Drupal is written procedurally (at least they admit it) which is not my style. Something always goes wrong when I try to install EzPublish.
Then of course there is the option of RDBMSWAAS. When thinking about that last option, there are two frameworks that come to mind, Symfony, and Django. I tried Django before for a small project, and it worked out well, but I had never really given Symfony the time of day. When looking at the Symfony website, I stumbled across a nice hidden gem. Read the rest of this entry »
Quick code review of Expression Engine 2.0
Posted by Avi in Programming on December 29th, 2009
I just got a hold of the public beta of Expression Engine 2.0. I have been using Expression Engine 1.x for a number of years now, and I’ll admit, I wasn’t the most experienced PHP programmer at the time. It was probably the first large PHP program that I had ever used, and I was a little bit awed at how such a massively big OO program could be written.
Since then, I’ve improved my skills tremendously. I’ve learned about SOLID, and DRY. Dependency Injection and Test driven design. I’ve learned about domain modeling, and proper OO design. The code in EE 1.x is utter crap, of course! Global variables scattered everywhere. Copy & Paste style coding. Non-resuable components. No coherent API to speak of. Purely designed plugin system. Who could blame them, though. The code was written almost 10 years ago, in PHP4–with no intention of dropping PHP4 support.
I think I first heard about EE 2.0 almost two years ago. It was coming out that summer. Ever little bit of news that came out made me a little more gittier. It was going to be built on Code Igniter, an MVC framework. Boy was I suprised. Read the rest of this entry »
CQRS (Command and Query Responsibility Segregation) in PHP
Posted by Avi in Programming on December 10th, 2009
It always seems to me that the PHP world is one step behind the current trends in the “enterprise world”. For example, Rails popularized the “ActiveRecord” pattern a few years ago. Ever since then, there has been an explosion of ActiveRecord implementations in PHP. Many even mistakenly refer to them as an “ORM”, but I won’t beat a dead horse here. ActiveRecord is great for your basic run-of-the-mill address book/cookbook/blog type of app, where it basically data driven, and there’s not much “business logic” to go around. For the majority of PHP applications out there, these tools are a god send.
As PHP matures, especially the object model, advanced developers have started to realize the limits of this pattern. Now the big rage is the DataMapper pattern, and DDD. We try to separate out the domain of application, and the holy grail is now transparent persistence. With great tools like Doctrine 2.0 and Object_Freezer, both of which are only possible with the new additions in PHP 5.3, these dreams are becoming a reality.
Read the rest of this entry »
Alternative to Zend_Form ViewScript decorator
Posted by Avi in Programming on November 29th, 2009
Zend_Form is great for prototyping forms. It takes a lot of the drudge work out, and makes forms easily “reusable”. The decorator system, which is admittedly a little scary for the uninitiated, is actually very powerful. I’m suer that eventually, however, we’ve all reached that point. The point where you spend more time configuring the form to do what you want, then actually doing something with it. Read the rest of this entry »
Making Zend_Auth more “Object Oriented”
Posted by Avi in Programming on October 29th, 2009
Recently Giorgio Sironi wrote a post on Zend_Auth. When I saw the title, I was expecting something along the lines of most of his posts, which have been discussing many OO topics. The content was rather disappointing, and I’ll tell you why.
Read the rest of this entry »