<?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/"
	>

<channel>
	<title>The World Of Gavin &#187; attributeMap</title>
	<atom:link href="http://www.gavinwillingham.com/tag/attributemap/feed" rel="self" type="application/rss+xml" />
	<link>http://www.gavinwillingham.com</link>
	<description>My opinions on things I have opinions on</description>
	<lastBuildDate>Fri, 28 Jan 2011 17:08:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Dojo AttributeMap</title>
		<link>http://www.gavinwillingham.com/dojo-attributemap.html</link>
		<comments>http://www.gavinwillingham.com/dojo-attributemap.html#comments</comments>
		<pubDate>Sun, 28 Feb 2010 10:52:46 +0000</pubDate>
		<dc:creator>gavin</dc:creator>
				<category><![CDATA[Content]]></category>
		<category><![CDATA[attributeMap]]></category>
		<category><![CDATA[dojo]]></category>

		<guid isPermaLink="false">http://www.gavinwillingham.com/?p=758</guid>
		<description><![CDATA[When using Dojo templating, most guides on the web suggest using ${variable} in your template, where variable is the name of one of your widget&#8217;s attributes. When writing my first custom widget, I couldn&#8217;t get this to work; no matter what I tried the templating engine would not replace ${} blocks. Frantically searching for a [...]]]></description>
			<content:encoded><![CDATA[<p>When using Dojo templating, most guides on the web suggest using $<code>{variable}</code> in your template, where <code>variable</code> is the name of one of your widget&#8217;s attributes. When writing my first custom widget, I couldn&#8217;t get this to work; no matter what I tried the templating engine would not replace <code>${}</code> blocks.</p>
<p>Frantically searching for a solution, I found numerous references to using an &#8220;attribute map&#8221; instead of <code>${}</code>, but the documentation was pretty poor.</p>
<p>Basically, you can add an attribute map by declaring an attribute in your widget called <code>attributeMap</code>. You then perform a mapping between attribute name in your widget and node in your template.</p>
<pre>dojo.declare(
    'customWidgets.MyWidget',
    [dijit._Widget, dijit._Templated],
    {
        variable : 'myVar',

        attributeMap : {
            // key is attribute name, value is node identifier
            variable : 'myNode',
        },

        templatePath :
            new dojo.moduleUrl('customWidgets', 'MyWidget.html')
    }
);</pre>
<p>This seems pretty straightforward. Except&#8230; well:</p>
<ol>
<li>What is <code>myNode</code></li>
<li>What part of <code>myNode</code> does it bind to?</li>
</ol>
<p>In your template, you need to declare a <code>dojoAttachPoint</code>, which will become your node. This is pretty straightforward:</p>
<pre>&lt;div dojoAttachPoint="myNode"&gt;&lt;/div&gt;</pre>
<p>For point 2, what this will actually do, is create an HTML attribute called <code>variable</code> in your <code>&lt;div&gt;</code>, i.e.</p>
<pre>&lt;div variable="myVar"&gt;&lt;/div&gt;</pre>
<p>This may or may not be useful. You can invoke this behaviour more explicitly </p>
<pre>
        attributeMap : {
            variable : {
                node : 'myNode',
                type : 'attribute'
            }
        },
</pre>
<p>If you want the <code>div</code>&#8216;s <i>contents</i> to take your value, you can set <code>type</code> to <code>innerHTML</code>. The bit of info that&#8217;s useful to know, however, is that you can add a third property called <code>attribute<code>:</p>
<pre>
        attributeMap : {
            variable : {
                node : 'myNode',
                type : 'attribute',
                attribute : 'alt'
            }
        },
</pre>
<p>In this example, <code>variable</code> will be mapped to the <code>alt</code> property of <code>myNode</code>. Very useful if you want to set a <code>src</code> on 2 separate images, for example.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gavinwillingham.com/dojo-attributemap.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

