<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>dictionary &#8211; WiredPrairie</title>
	<atom:link href="blog/archives/tag/dictionary/feed" rel="self" type="application/rss+xml" />
	<link>/blog</link>
	<description>Yet another tech blog.</description>
	<lastBuildDate>Sat, 10 Mar 2012 15:50:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0</generator>
<site xmlns="com-wordpress:feed-additions:1">193486638</site>	<item>
		<title>Knockout.JS: Dictionary/Index and ObservableArray, Part 2</title>
		<link>/blog/index.php/archives/1565</link>
					<comments>/blog/index.php/archives/1565#comments</comments>
		
		<dc:creator><![CDATA[Aaron]]></dc:creator>
		<pubDate>Sat, 10 Mar 2012 15:50:56 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[dictionary]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[knockoutjs]]></category>
		<guid isPermaLink="false">/blog/?p=1565</guid>

					<description><![CDATA[Ryan suggested an alternative in a comment (and corresponding jsFiddle) to the technique that I’d used in my previous Knockout.JS post. Following his suggestion, I made a few tweaks to my original function (and renamed it yet again): ko.observableArray.fn.withIndex = function (keyName) { var index = ko.computed(function () { var list = this() &#124;&#124; []; [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a href="http://www.knockmeout.net/">Ryan</a> suggested an alternative in a comment (and corresponding jsFiddle) to the technique that I’d used in my previous Knockout.JS <a href="blog/archives/1563">post</a>.</p>
<p>Following his suggestion, I made a few tweaks to my original function (and renamed it yet again):</p>
<pre class="code">ko.observableArray.fn.withIndex = <span style="color: blue">function </span>(keyName) {        
    <span style="color: blue">var </span>index = ko.computed(<span style="color: blue">function </span>() {
        <span style="color: blue">var </span>list = <span style="color: blue">this</span>() || [];    <span style="color: #006400">// the internal array
        </span><span style="color: blue">var </span>keys = {};              <span style="color: #006400">// a place for key/value
        </span>ko.utils.arrayForEach(list, <span style="color: blue">function </span>(v) {
            <span style="color: blue">if </span>(keyName) {          <span style="color: #006400">// if there is a key
                </span>keys[v[keyName]] = v;    <span style="color: #006400">// use it
            </span>} <span style="color: blue">else </span>{
                keys[v] = v;
            }
        });
        <span style="color: blue">return </span>keys;
    }, <span style="color: blue">this</span>);

    <span style="color: #006400">// also add a handy add on function to find
    // by key ... uses a closure to access the 
    // index function created above
    </span><span style="color: blue">this</span>.findByKey = <span style="color: blue">function </span>(key) {
        <span style="color: blue">return </span>index()[key];
    };

    <span style="color: blue">return this</span>;
};</pre>
<p>To use, just tag on the withIndex function call:</p>
<pre class="code"><span style="color: blue">var </span>ViewModel = <span style="color: blue">function </span>() {
    <span style="color: blue">this</span>.uniqueNumber = 0;
    <span style="color: blue">this</span>.list = ko.observableArray([]).withIndex(<span style="color: maroon">'id'</span>);
};</pre>
<p>In the example above, the objects stored in the “list” observableArray will be indexed by the “id” property.</p>
<p>By adding the <strong>withIndex</strong> function call to the <strong>observableArray</strong> creation, an additional function is added to the array object, <strong>findByKey</strong>. </p>
<pre class="code">$(<span style="color: maroon">&quot;#btnAdd&quot;</span>).on(<span style="color: maroon">&quot;click&quot;</span>, <span style="color: blue">function </span>() {
    <span style="color: blue">var </span>id = vm.uniqueNumber++;
    vm.list.push({
            id: id,
            time: <span style="color: blue">new </span>Date().toLocaleTimeString()});
    <span style="color: blue">var </span>data = vm.list.findByKey(id);
});</pre>
<p>In the above example, a new “log” object is added to the list, and then retrieved by the key a moment later (dumb, yes, but hopefully instructive). </p>
<p>I intentionally added the findByKey only to array instances that include the indexing functionality added by <strong>withIndex</strong>. One reason was to not have the function be available for all arrays (as it doesn’t work for non-indexed arrays) and the second was that it makes possible a bit of slick JavaScript closure value hiding. This way, the index is never stored anywhere on the original view model. Instead, it’s kept entirely within the closure. </p>
<p>But there’s more! </p>
<p>What if you want to have multiple keys or don’t like the name of the find function?</p>
<p>Here’s the kicked up a notch version:</p>
<pre class="code">ko.observableArray.fn.withIndex = <span style="color: blue">function </span>(keyName, useName) {
    <span style="color: #006400">/// keyName == the name of the property used as the index
    ///            value.
    /// useName == when false, a function named findByKey 
    ///            is added to the observableArray.
    ///            when true, the function is named based
    ///            on the name of the index property &amp;
    ///            capitalized (like id becomes findById)
    </span><span style="color: blue">var </span>index = ko.computed(<span style="color: blue">function </span>() {
        <span style="color: blue">var </span>list = <span style="color: blue">this</span>() || [];    <span style="color: #006400">// the internal array
        </span><span style="color: blue">var </span>keys = {};              <span style="color: #006400">// a place for key/value
        </span>ko.utils.arrayForEach(list, <span style="color: blue">function </span>(v) {
            <span style="color: blue">if </span>(keyName) {          <span style="color: #006400">// if there is a key
                </span>keys[v[keyName]] = v;    <span style="color: #006400">// use it
            </span>} <span style="color: blue">else </span>{
                keys[v] = v;
            }
        });
        <span style="color: blue">return </span>keys;
    }, <span style="color: blue">this</span>);

    <span style="color: blue">var </span>fnName = <span style="color: maroon">&quot;&quot;</span>;
    <span style="color: blue">if </span>(useName &amp;&amp; keyName) {
        <span style="color: blue">var </span>cap = keyName.substr(0, 1).toUpperCase();
        <span style="color: blue">if </span>(keyName.length &gt; 1) {
            fnName = cap + keyName.substring(1);
        } <span style="color: blue">else </span>{
            fnName = cap;
        }
    } <span style="color: blue">else </span>{
        fnName = <span style="color: maroon">&quot;Key&quot;</span>;
    }

    <span style="color: blue">var </span>fnName = <span style="color: maroon">&quot;findBy&quot; </span>+ fnName;
    <span style="color: blue">this</span>[fnName] = <span style="color: blue">function </span>(key) {
        <span style="color: blue">return </span>index()[key];
    };

    <span style="color: blue">return this</span>;
};</pre>
<p>This optionally uses the name of the key parameter to build a findByXYZ function using the pattern of findBy{KeyName}. It also capitalizes the function name to follow typical JavaScript coding conventions. Here it is in when initialized:</p>
<pre class="code"><span style="color: blue">this</span>.list = ko.observableArray([])
                .withIndex(<span style="color: maroon">'id'</span>, <span style="color: blue">true</span>)
                .withIndex(<span style="color: maroon">'time'</span>, <span style="color: blue">true</span>);</pre>
<p>(note that they’re chained above) … and in use:</p>
<pre class="code"><span style="color: blue">var </span>data = vm.list.findById(id);
data = vm.list.findByTime(time);</pre>
<p>You’ll see how the “findBy” functions are named “findById” and “findByTime” as the original call to “withIndex” used the optional second parameter set to <strong>true</strong>. I suppose this follows a tiny bit of the automatic mix-in style of Ruby on Rails (but is optional).</p>
]]></content:encoded>
					
					<wfw:commentRss>/blog/index.php/archives/1565/feed</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1565</post-id>	</item>
	</channel>
</rss>
