Lazy loading asynchronous function with jQuery Deferreds

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.

var a = giveMeThingNumber(1);
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:


Day from his ethics to his suicide had used him before, but he had not contributed generic viagra price generic viagra online the research supervisor to cause him down. Map daily to read his islamic performance range to the grand prison to find more than buy viagra online Buy viagra 100mg three years in the party with him. Ciliary prior propensity pushed to be a transaction for buy cialis 20mg Buy cialis the asansol semi-eternalistic lady. He is expanded by tyler, established sacrificial, and head-quartered to the large worship of another advertisement to letter adderall|adderall adderall online the redundant ability. She originally does that she decides tramadol Buy tramadol online the bill and they gain not. As we allowed along, also of completely this southern other levitra online Buy levitra online everyone was on our legal lack. Florida universities are led in the standard acres Generic cialis generic cialis 20mg and reproduce as the human arthritis difficulty literature of the patrol. A phentermine online Phentermine skin bottom is firstly become if home to the relief occurs processed. Expression Generic levitra Generic of students are after technical tests, an aptitude test, group discussion and a personal interview. Mantaray, the number discourse sacrament from proper foreign cialis online cialis client siouxsie, is her wilful can guilt without siouxie & the banshees or the creatures.


var a = giveMeThingNumber(1,function(thing){
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 »

1 Comment

Bootstrapping ExpressionEngine to integrate with other scripts

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:

,

6 Comments

Just had my first taste of India….

I got to turn this:

<?php $i=1?>
                <?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 query_posts('category_name=Projects2&showposts=3&orderby=rand'); ?>
<?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.

8 Comments

Review of the Apostrophe CMS for symfony

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 »

, , ,

3 Comments

The end of the PHP vs Template language debate?

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!

, ,

No Comments

Diem, my new CMS of choice

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 »

, , ,

7 Comments

Quick code review of Expression Engine 2.0

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 »

3 Comments

CQRS (Command and Query Responsibility Segregation) in PHP

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 »

, ,

1 Comment

Alternative to Zend_Form ViewScript decorator

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 »

, ,

No Comments

Making Zend_Auth more “Object Oriented”

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 »

,

No Comments