Tag: foreach

  • Convert the (un)check all to a #KnockoutJS Component

    You’ve created a nice feature leveraging Knockout.js and now you want to re-use this feature on another page, another site, or in a slightly different fashion. This example will extend the previous (Un)Check All using Knockout.js and make it easily re-usable.

    A Knockout component can be created using a mixture of HTML (with data bindings) and a Knockout ViewModel. By altering the previous check all example and making it slightly more re-usable, it can be easily added to other pages with minimal effort. If you are not already familiar with the example being extended, please take a minute and give the Knockout js tutorial a quick read with the link provided above.

    Once the component is created, it can be included on any page with this simple HTML:

    <checkall params=”items: items, selectedItems: selectedItems”></checkall>

    (more…)

  • (Un)Check All using #KnockoutJS

    When I wrote Knockout.js: Building Dynamic Client-Side Web Applications I was trying to focus on demonstrating specific things, such as custom bindings, extending observables, etc. Unfortunately this didn’t leave room for what I would call “random” examples of things that I do on a semi-regular basis. This blog post will demonstrate how to create a (un)check all list of checkboxes.

    If I haven’t said it before, Knockout js examples like this are why I love working with this framework on a daily basis. This example is accomplished in under 50 lines of code, with most of it being whitespace for readability!

    The following example assumes you understand how to install Knockout.js and have a brief understanding of Knockout ViewModels. If you are looking for a good introduction *cough* *cough*, my book does an excellent job of it 😉

    (more…)

  • A Short Rant About Coding Conventions

    Yesterday’s article actually got me a little amped up about coding conventions – Comparing a while loop against a foreach loop of an array – because I never thought I would actually have to do a comparison between a while loop and a foreach loop on an array!  If we go back and revisit the post, I was reviewing a recent CakePHP commit for an optimization on the Hash class.  The code in question is three separate blocks of code that leverage the array_shift function to get the next value in the array with a foreach loop instead.

    (more…)

  • Comparing a while loop against a foreach loop of an array

    Are you confused by the title?  I was when I first got the idea to even write this blog as well.  I was recently perusing the CakePHP change logs and came across an interesting commit – Optimization for Hash Method.

    The optimization is quite simple, this code:

    [code]
    while (($key = array_shift($parts)) !== null)
    [/code]

    Is replaced with the following code:

    [code]
    foreach ($parts as $key)
    [/code]

    This is actually done several times in the commit.  It seems that the original developer is really keen on using the array_shift function.  Just in seeing this code, I thought the original code was odd to utilize that function so I immediately had to do a comparison and validate the optimization!

    (more…)