Category: jQuery

Aug 14 2008

Chaining objects in Reactor, an idea

I have been doing a fair amount of work recently with Reactor, and not so related to reactor, I have also been using JQuery. This has led me to a little thought experiment I was going to propose to Doug Hughes for the project, which is the idea of chaining. This is something that JQuery does fairly well and Reactor does to some level. The idea is this, that in any object that has setters, or functions that return "void", they should actually return the newly modified object itself ( the "this" scope). "Why?" you might ask, well, here are a couple of examples from my code: <cfscript>
      orm = getModelGlue().getOrmService();
      myObj = orm.createRecord("Element");
      myObj.setId("124");
      myObj.load();
      
      //do stuff with the object now
   
   </cfscript>
Well, that is ok, but I would prefer to do the creating the object and loading it in one line: <cfscript>
      orm = getModelGlue().getOrmService();
      myObj = orm.createRecord("Element").setId("124").load();
      //do stuff with the object now
   
   </cfscript>
There we go, in one line. This could also go for doing all the setting of the other properties: <cfscript>
      orm = getModelGlue().getOrmService();
      myObj = orm.createRecord("Element")
               .setId("124")
               .load()
               .setName("Elvis")
               .setAge("34")
               .save();
   </cfscript>
What do people think of this idea? It is fairly easily implemented, doesn't break any existing code and would remove a lot of <cfset>'s and if nicely formatted, make just as much sense. EDIT: As an aside, Doug let me know (and I should have known really but its nice to learn a new thing every day) that you can shorten the loading even further: <cfscript>
      orm = getModelGlue().getOrmService();
      myObj = orm.createRecord("Element").load(id="124");
   </cfscript>

9 comments - Posted by Mark Drew at 4:36 PM - Categories: coldfusion | reactor | jQuery

Search Archives

Contribute


MD on Twitter

Categories

coldfusion cfeclipse personal apple cfconferences ukcfug model-glue webdev blogging cfunited coldspring railo presentations adobe cfobjective scotch on the rocks reactor lost jobs ajax eclipse flex model-glue frameworks max google podcast podcasting reference ask md fusebox getrailo spam donations eventvalidation fusiondebug air blue dragon cloud development flying funny java microsoft music snipex tattoo tips apple aptana blog cfml cfml derby eventguard graphic novels itunes metrics open bluedragon rant wishlist apollo barcamp bolt cebit cfdevcon cfonwheels cfug codeshare codeswarm coldfusioncamp directing doh! firefox flash gadgets groovy hosting jquery mdc mvc ooops pownce quack regex spry subversion tashr transfer webservices xml

Monthly Archives