<?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>At the end of the Web</title>
	<atom:link href="http://martinhansen.no/feed/" rel="self" type="application/rss+xml" />
	<link>http://martinhansen.no</link>
	<description>meh</description>
	<lastBuildDate>Sat, 07 Apr 2012 18:47:39 +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>A model for Knockout</title>
		<link>http://martinhansen.no/2012/04/07/a-model-for-knockout/</link>
		<comments>http://martinhansen.no/2012/04/07/a-model-for-knockout/#comments</comments>
		<pubDate>Sat, 07 Apr 2012 16:39:05 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[javascriptmvc]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[knockout]]></category>
		<category><![CDATA[knockout-mapping]]></category>
		<category><![CDATA[knockoutmodel]]></category>

		<guid isPermaLink="false">http://martinhansen.no/?p=329</guid>
		<description><![CDATA[I have created a model for knockout inspired/based off of JavaScriptMVC&#8217;s Model. Go to the github project page for updated code and documentation h1,h2,h3,h4,h5,h6{ font-weight:normal; color:#111; line-height:1em; } h4,h5,h6{ font-weight: bold; } h1{ font-size:2.5em; } h2{ font-size:2em; } h3{ font-size:1.5em; } h4{ font-size:1.2em; } h5{ font-size:1em; } h6{ font-size:0.9em; } blockquote{ color:#666666; margin:0; padding-left: 3em; [...]]]></description>
			<content:encoded><![CDATA[<p>I have created a model for knockout inspired/based off of JavaScriptMVC&#8217;s Model.</p>
<p>Go to the github <a href="https://github.com/mokkabonna/knockout-model">project page</a> for updated code and documentation</p>
<style>
h1,h2,h3,h4,h5,h6{
font-weight:normal;
color:#111;
line-height:1em;
}
h4,h5,h6{ font-weight: bold; }
h1{ font-size:2.5em; }
h2{ font-size:2em; }
h3{ font-size:1.5em; }
h4{ font-size:1.2em; }
h5{ font-size:1em; }
h6{ font-size:0.9em; }</p>
<p>blockquote{
color:#666666;
margin:0;
padding-left: 3em;
border-left: 0.5em #EEE solid;
}
hr { display: block; height: 2px; border: 0; border-top: 1px solid #aaa;border-bottom: 1px solid #eee; margin: 1em 0; padding: 0; }</p>
<p>pre , code, kbd, samp { 
  color: #000; 
  font-family: monospace; 
  font-size: 0.98em; 
  border-radius:3px;
  background-color: #F8F8F8;
  border: 1px solid #CCC; 
}
pre { white-space: pre; white-space: pre-wrap; word-wrap: break-word; padding: 5px;}
pre code { border: 0px !important; }</p>
<p>b, strong { font-weight: bold; }</p>
<p>dfn { font-style: italic; }</p>
<p>ins { background: #ff9; color: #000; text-decoration: none; }</p>
<p>mark { background: #ff0; color: #000; font-style: italic; font-weight: bold; }</p>
<p>sub, sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; }
sup { top: -0.5em; }
sub { bottom: -0.25em; }</p>
<p>ul, ol { margin: 1em 0; padding: 0 0 0 2em; }
li p:last-child { margin:0 }
dd { margin: 0 0 0 2em; }</p>
<p>img { border: 0; -ms-interpolation-mode: bicubic; vertical-align: middle; }</p>
<p>table { border-collapse: collapse; border-spacing: 0; }
td { vertical-align: top; }</p>
<p>@media only screen and (min-width: 480px) {
body{font-size:14px;}
}</p>
<p>@media only screen and (min-width: 768px) {
body{font-size:16px;}
}</p>
<p>@media print {
  * { background: transparent !important; color: black !important; filter:none !important; -ms-filter: none !important; }
  body{font-size:12pt; max-width:100%;}
  a, a:visited { text-decoration: underline; }
  hr { height: 1px; border:0; border-bottom:1px solid black; }
  a[href]:after { content: " (" attr(href) ")"; }
  abbr[title]:after { content: " (" attr(title) ")"; }
  .ir a:after, a[href^="javascript:"]:after, a[href^="#"]:after { content: ""; }
  pre, blockquote { border: 1px solid #999; padding-right: 1em; page-break-inside: avoid; }
  tr, img { page-break-inside: avoid; }
  img { max-width: 100% !important; }
  @page :left { margin: 15mm 20mm 15mm 10mm; }
  @page :right { margin: 15mm 10mm 15mm 20mm; }
  p, h2, h3 { orphans: 3; widows: 3; }
  h2, h3 { page-break-after: avoid; }
}</style>
<h1>Knockout Model</h1>
<p>A model for <a href="https://github.com/SteveSanderson/knockout">knockout</a></p>
<p>Inspired by and partially based on <a href="http://javascriptmvc.com/docs.html#!jQuery.Model">JavaScriptMVC&#8217;s Model</a></p>
<p>Also uses the jQuery.Class from that project. </p>
<p>Dependencies:</p>
<ul>
<li>jQuery</li>
<li>jQuery Class</li>
<li>Knockout</li>
<li>Knockout mapping</li>
</ul>
<p>Built with ASP.NET Web API in mind, so it assumes some conventions:</p>
<ul>
<li>by default all url start with /api</li>
<li>urls are made up by the lowercase, pluralized name of the model, Person model =&gt; /api/persons</li>
<li>Built in conversion of ASP.NET JSON date format  \/Date(1224043200000)\/ </li>
<li>the id of the model defaults to Id</li>
</ul>
<p><strong>All conventions can however be overridden and you can use it with any server side framework</strong></p>
<h2>Usage</h2>
<pre><code>$.KnockoutModel('Person', {}, {});
</code></pre>
<p>This assumes a full REST way of accessing that model on the server:</p>
<pre><code>GET /api/persons //Lists all persons
GET /api/persons/1 //Get person with id 1
POST /api/persons //Create a new person
PUT /api/persons/1 //Update person with id 1
DELETE /api/persons/1 //Delete person with id 1
</code></pre>
<p><strong>Find all</strong></p>
<pre><code>Person.findAll().done(function(persons){
    //do things with the persons array
});
</code></pre>
<p>query with parameters (OData url conventions in example)</p>
<pre><code>var page = 5,
    pagesize = 10,
Person.findAll({$top: 10, $skip: 20}).done(function(persons){
    //persons 21-30
});
</code></pre>
<p><strong>Find One</strong></p>
<pre><code>Person.findOne({Id : 3}).done(function(person){
    //do things with person
});
</code></pre>
<p><strong>Create new model</strong></p>
<pre><code>var person =  new Person({ Name:'John', Age: 15 });
</code></pre>
<p><strong>Save</strong></p>
<pre><code>person.save().done(function(){
    //person is now updated/created on the server
});
</code></pre>
<p><strong>Destroy</strong></p>
<pre><code>person.destroy().done(function(){
    //person is now deleted on the server
});
</code></pre>
<p><strong>Tracking status</strong></p>
<p>Every instance will have a property <em>isModified</em> (observable as well) that will change whenever any other observable in the model is changed. This also works with save, when something is saved, the modified status is checked up against the new values returned from the database.</p>
<p><strong>Mapping</strong></p>
<p>By default all properties are observable and part of the main object. However you can define specific mapping options that follow standard knockout mapping standards. </p>
<p>The following example doesn&#8217;t make observables of the Id, Created and Updated properties.</p>
<pre><code>$.KnockoutModel('Person', {
    mapping : {
        'copy' : ['Id', 'Created', 'Updated']
    }
}, {});
</code></pre>
<p>If the server returns a model with a childmodel you can also combine models like so:</p>
<pre><code>$.KnockoutModel('Address', {
    mapping : {
        'copy' : ['Id']
    }
}, {});

$.KnockoutModel('Person', {
    mapping: {
        'Address': {
            create: function(options) {
                return new Address(options.data)
            }
        }
    }
}, {});
</code></pre>
<p>This will make the Address property of Person a separate class, with it&#8217;s own methods like save, destroy etc. Also on Address, we choose to not make observable of the property Id;</p>
<p>This way you can call save on a person&#8217;s address to only update that if you please.</p>
<h2>Model options</h2>
<ul>
<li>findAll</li>
<li>findOne</li>
<li>create</li>
<li>update</li>
<li>destroy</li>
</ul>
<p><em>These methods are automatically created from the model name and api prefix as shown above, however they can be overridden with a templated string. For instance: update : &#8216;POST /api/persons/{Id}&#8217; to use POST instead of PUT for updates.</em></p>
<ul>
<li>
<p>apiPrefix : &#8216;/admin/api&#8217;</p>
<ul>
<li>default : &#8216;/api&#8217;</li>
</ul>
</li>
<li>
<p>id : &#8216;id&#8217;</p>
<ul>
<li>default : &#8216;Id&#8217;</li>
</ul>
</li>
</ul>
<p><em>this is important to set if the id of your model is something else than &#8216;Id&#8217;, as the existence of it determines determines wheter the model creates or updates when calling save.</em></p>
<ul>
<li>resourceName : &#8216;fishes&#8217;
<ul>
<li>default : model name + &#8216;s&#8217;, person =&gt; persons</li>
</ul>
</li>
</ul>
<p><em>The model only does simple pluralization (adds an &#8216;s&#8217;), in cases where other forms is needed or where you simply want to call the model something else than the service url, set the resourceName property.</em></p>
<p><strong>Note:</strong> In case of missing pieces in this simple documentation, your best bet is to check out the <a href="http://javascriptmvc.com/docs.html#!jQuery.Model">documentation for JavaScriptMVC&#8217;s model</a> and for mapping specifically check out the documentation for the <a href="http://knockoutjs.com/documentation/plugins-mapping.html">mapping plugin</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://martinhansen.no/2012/04/07/a-model-for-knockout/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Twitter might give your users MB&#8217;s of extra data to download</title>
		<link>http://martinhansen.no/2012/02/20/twitter-might-give-your-users-mbs-of-extra-data-to-download/</link>
		<comments>http://martinhansen.no/2012/02/20/twitter-might-give-your-users-mbs-of-extra-data-to-download/#comments</comments>
		<pubDate>Mon, 20 Feb 2012 12:31:17 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[web]]></category>
		<category><![CDATA[compression]]></category>
		<category><![CDATA[exif]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://martinhansen.no/?p=315</guid>
		<description><![CDATA[When someone uploads their profile picture to twitter, twitter has a limitation of 700 KB. Ok. Twitter then compresses the image to something smaller, like a 48&#215;48 px image. The size can typically be a few KB&#8217;s. Good! However twitter does not remove any thumbnail data that is embedded in the image (typically photoshop&#8217;s fault). [...]]]></description>
			<content:encoded><![CDATA[<p>When someone uploads their profile picture to twitter, twitter has a limitation of 700 KB. Ok.<br />
Twitter then compresses the image to something smaller, like a 48&#215;48 px image. The size can typically be a few KB&#8217;s. Good!<br />
However twitter does not remove any thumbnail data that is embedded in the image (typically photoshop&#8217;s fault). This might result in images much much bigger than a few KB&#8217;s. Fail!</p>
<p>This of course causes problems for websites that embed twitter widgets, or any twitter client for that matter, even the twitter website itself. I&#8217;ve seen in the wild 48&#215;48 px images that is 500 KB+, if you are unlucky and many people in your twitter widget on your website have used photoshop to crop their images without using &#8220;save for web&#8221; (which removes the thumbnail data) then your users might end up downloading MB&#8217;s of unnecessary data.</p>
<p>Not to mention that twitter&#8217;s bandwidth bill goes up because of many images being 100+ times bigger than they need to be.</p>
<p>Please fix this twitter.</p>
<p>Kthxbye <img src='http://martinhansen.no/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://martinhansen.no/2012/02/20/twitter-might-give-your-users-mbs-of-extra-data-to-download/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create hierarchical objects from flat data</title>
		<link>http://martinhansen.no/2012/02/19/create-hierarchical-objects-from-flat-data/</link>
		<comments>http://martinhansen.no/2012/02/19/create-hierarchical-objects-from-flat-data/#comments</comments>
		<pubDate>Sun, 19 Feb 2012 20:17:23 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[hierarchy]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[linq.js]]></category>

		<guid isPermaLink="false">http://martinhansen.no/?p=279</guid>
		<description><![CDATA[Typically when you have a menu or similar, you might have it organized as following, with several levels hierarchical: Home - Products - Hardware - About us Let&#8217;s say you get the data from the server in JSON format like this var menudata = [{ id: 1, name: &#039;Frontpage&#039;, parentId: null }, { id: 2, [...]]]></description>
			<content:encoded><![CDATA[<p>Typically when you have a menu or similar, you might have it organized as following, with several levels hierarchical:</p>
<pre>
Home
 - Products
  - Hardware
 - About us
</pre>
<p>Let&#8217;s say you get the data from the server in JSON format like this</p>
<pre class="brush: javascript">
var menudata = [{
	id: 1,
	name: &#039;Frontpage&#039;,
	parentId: null
}, {
	id: 2,
	name: &#039;Products&#039;,
	parentId: 1
}, {
	id: 3,
	name: &#039;Hardware&#039;,
	parentId: 2
},{
	id: 4,
	name: &#039;About us&#039;,
	parentId: 1
}];
</pre>
<p>To make this structured in the desired way, I added a method ByHierarchy to <a href="http://linqjs.codeplex.com/">linq.js</a>, an excellent javascript library for easy manipulation of data.</p>
<p>You use it like this:</p>
<pre class="brush: javascript">
Enumerable.From(menudata).ByHierarchy(function(d) {
	return d.parentId == null;
}, function(parent, child) {
	return parent.id == child.parentId;
}).ToArray();
</pre>
<p>The first parameter determines what elements should be on the first level. In this example we want all elements that have no parent to be on the first level.<br />
The second parameter tells you how to connect the deeper levels.</p>
<p>The above code will result in the following:</p>
<pre class="brush: javascript">
[
  {
    &quot;item&quot;: {
      &quot;id&quot;: 1,
      &quot;name&quot;: &quot;Frontpage&quot;,
      &quot;parentId&quot;: null
    },
    &quot;level&quot;: 1,
    &quot;children&quot;: [
      {
        &quot;item&quot;: {
          &quot;id&quot;: 2,
          &quot;name&quot;: &quot;Products&quot;,
          &quot;parentId&quot;: 1
        },
        &quot;level&quot;: 2,
        &quot;children&quot;: [
          {
            &quot;item&quot;: {
              &quot;id&quot;: 3,
              &quot;name&quot;: &quot;Hardware&quot;,
              &quot;parentId&quot;: 2
            },
            &quot;level&quot;: 3,
            &quot;children&quot;: []
          }
        ]
      },
      {
        &quot;item&quot;: {
          &quot;id&quot;: 4,
          &quot;name&quot;: &quot;About us&quot;,
          &quot;parentId&quot;: 1
        },
        &quot;level&quot;: 2,
        &quot;children&quot;: []
      }
    ]
  }
]
</pre>
<p>(Note that the object on each level also will have a function called &#8220;getParent&#8221;, that will return the parent if it exists, here it is not visible as this is serialized using JSON.stringify, which excludes functions)</p>
<p>You can also use this in other ways, say an array of numbers: </p>
<pre class="brush: javascript">
Enumerable.From([2,4,5,9,3,25,16,64,8,81]).ByHierarchy(function(d) {
	return d &lt; 10;
}, function(parent, child) {
	return parent * parent == child;
});
</pre>
<p>This will result in a root level of all the numbers less than 10 and the children will be any number that is the parent number squared, and their children will be the same etc. until there is no more numbers. The already existing methods CascadeDepthFirst and CascadeBreadthFirst. Here is an example with CascadeDepthFirst:</p>
<pre class="brush: javascript">
Enumerable.From([2,4,5,9,3,25,16,64,8,81]).ByHierarchy(function(d) {
	return d &lt; 10;
}, function(parent, child) {
	return parent * parent == child;
}).OrderBy(&#039;$.item&#039;).CascadeDepthFirst(&#039;$.children&#039;, &#039;$.item&#039;).ToArray();
</pre>
<p>(The first parameter to CascadeDepthFirst is the parameter that holds the children, and the other is what to select from each child)</p>
<p>This will produce the following :</p>
<pre class="brush: javascript">
[2,4,16,3,9,81,4,16,5,25,8,64,9,81]
</pre>
<p>Be aware that any method like OrderBy or Where etc is only applied to the top level. Here is an example with Where:</p>
<pre class="brush: javascript">
Enumerable.From([2,4,5,9,3,25,16,64,8,81]).ByHierarchy(function(d) {
	return d &lt; 10;
}, function(parent, child) {
	return parent * parent == child;
}).Where(&#039;$.item &lt; 4&#039;).ToArray();
</pre>
<p>This will filter so that we only get 2, and 3 at the root level. However the children will still contain 4, 16 and 9, 81.</p>
<p>However if you change the code as follows then you will get only children less than 4 (none in this case).</p>
<pre class="brush: javascript">
 return parent * parent == child &amp;&amp; child &lt; 4;
</pre>
<p><strong>All parameters</strong></p>
<p><strong>firstLevel </strong>- elements that matches this condition will be on the first level<br />
<strong>connectBy </strong>- if this condition is met the item will be added as a child to the parent, be aware of possible endless loops.  returning true always will create endless children and fail. This method receives 2 arguments, parent, and child<br />
<strong>orderBy </strong>- a order expression to apply to the children at the time of creating the hierarchy. This does not sort the top level as well, that you have to do after.<br />
<strong>ascending </strong>- leave out or set to true for ascending sort or false to use descending sort.</p>
<p>The method will create new objects that have this structure:</p>
<pre class="brush: javascript">
{
    level : indicating the level of the current item,
    item : the original underlying data,
    children : contains any children,
    getParent : a method that returns the parent if any
}
</pre>
<p>Let me know if you have some problem or general feedback and I will try to answer them. I might also submit a pull request to the original project, and maybe they will include it as a standard method.</p>
<p><strong>More examples</strong></p>
<p>jQuery version:</p>
<pre class="brush: html">
&lt;div id=&quot;flatlists&quot;&gt;
	&lt;ul id=&quot;categories&quot; &gt;
		&lt;li data-subcategory=&quot;fruits&quot;&gt;Fruits&lt;/li&gt;
		&lt;li&gt;Clothes&lt;/li&gt;
	&lt;/ul&gt;

	&lt;ul id=&quot;fruits&quot;&gt;
		&lt;li data-subcategory=&quot;citrusfruits&quot;&gt;Citrus fruits&lt;/li&gt;
		&lt;li data-subcategory=&quot;plumfruits&quot;&gt;Plum fruits&lt;/li&gt;
	&lt;/ul&gt;

	&lt;ul id=&quot;plumfruits&quot;&gt;
		&lt;li&gt;Red plum&lt;/li&gt;
		&lt;li&gt;Yellow plum&lt;/li&gt;
	&lt;/ul&gt;

	&lt;ul id=&quot;citrusfruits&quot;&gt;
		&lt;li&gt;Lemon&lt;/li&gt;
		&lt;li&gt;Lime&lt;/li&gt;
		&lt;li&gt;Orange&lt;/li&gt;
	&lt;/ul&gt;
&lt;/div&gt;

&lt;ul id=&quot;resultlist&quot;&gt;

&lt;/ul&gt;
</pre>
<pre class="brush: javascript">
$(&#039;#flatlists li&#039;).toEnumerable().ByHierarchy(function(d) {
    return d.parent().attr(&#039;id&#039;) == &#039;categories&#039;;
}, function(parent, child) {
    return parent.attr(&#039;data-subcategory&#039;) == child.parent().attr(&#039;id&#039;);
}).CascadeDepthFirst(&#039;$.children&#039;).Select(&#039;$&#039;).ForEach(function(list) {

    list.item.css(&#039;padding-left&#039;, list.level * 10 + &#039;px&#039;);
    $(&#039;#resultlist&#039;).append(list.item);

});
</pre>
<p>Result:<br />
<img alt="" src="http://martinhansen.no/wp-content/uploads/listexample.png" class="alignnone" width="480" height="190" /></p>
<p>The forked project is hosted at codeplex (since the original is a codeplex project) You can find it here <a href="http://linqjs.codeplex.com/SourceControl/network/forks/mokkabonna/ByHierarchy">http://linqjs.codeplex.com/SourceControl/network/forks/mokkabonna/ByHierarchy</a></p>
<p>The code is inspired by <a href="http://stackoverflow.com/a/3758955/94394">this</a> similar LINQ C# method in an answer by Thomas Levesque over at stackoverflow.</p>
]]></content:encoded>
			<wfw:commentRss>http://martinhansen.no/2012/02/19/create-hierarchical-objects-from-flat-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASUS U36SD windows experience index score</title>
		<link>http://martinhansen.no/2011/10/05/asus-u36sd-windows-experience-index-score/</link>
		<comments>http://martinhansen.no/2011/10/05/asus-u36sd-windows-experience-index-score/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 14:31:39 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://martinhansen.no/?p=271</guid>
		<description><![CDATA[After getting my new laptop, an ASUS U36SD, weighting only 1,7 KG, replacing the HDD with an SSD and upgrading the RAM to 8 GB (I can&#8217;t believe how cheap ram is these days.) I ran the windows experience index test. These are the results I came up with. I think that is pretty good [...]]]></description>
			<content:encoded><![CDATA[<p>After getting my new laptop, an ASUS U36SD, weighting only 1,7 KG, replacing the HDD with an SSD and upgrading the RAM to 8 GB (I can&#8217;t believe how cheap ram is these days.) I ran the windows experience index test. These are the results I came up with.</p>
<p><a href="http://martinhansen.no/wp-content/uploads/experience.png"><img src="http://martinhansen.no/wp-content/uploads/experience.png" alt="" title="windows experience index score asus u36sd" width="653" height="198" class="aligncenter size-full wp-image-272" /></a></p>
<p>I think that is pretty good for a laptop of this size and price.</p>
]]></content:encoded>
			<wfw:commentRss>http://martinhansen.no/2011/10/05/asus-u36sd-windows-experience-index-score/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Format your json with userscript</title>
		<link>http://martinhansen.no/2011/09/08/format-your-json-with-userscript/</link>
		<comments>http://martinhansen.no/2011/09/08/format-your-json-with-userscript/#comments</comments>
		<pubDate>Thu, 08 Sep 2011 18:56:02 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[opera]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://martinhansen.no/?p=254</guid>
		<description><![CDATA[I have created a very small userscript for opera (might work ok with other browsers) that formats /indents/ beautifies the JSON data in a document if it&#8217;s only JSON there. For opera to open the JSON by itself and this to work (the default is to present it as a download), you will have to [...]]]></description>
			<content:encoded><![CDATA[<p>I have created a very small userscript for opera (might work ok with other browsers) that formats /indents/ beautifies the JSON data in a document if it&#8217;s only JSON there.</p>
<p>For opera to open the JSON by itself and this to work (the default is to present it as a download), you will have to enable that in opera. Go to preferences -> advanced -> downloads -> Add</p>
<p>Mimetype: application/json<br />
Select open with opera</p>
<p>That will result in your JSON looking like this in the browser:<br />
<a href="http://martinhansen.no/wp-content/uploads/large.png"><img src="http://martinhansen.no/wp-content/uploads/large.png" alt="" title="large" width="770" height="386" class="aligncenter size-full wp-image-265" /></a></p>
<pre class="brush: javascript">
// ==UserScript==
// @name 		   JSON formatter
// @version        1.0
// @description	   Formats JSON if the document only contains JSON
// @compability    Only tested with Opera, might work with others
// @author		   Martin Hansen
// @website        http://martinhansen.no
// ==/UserScript==
(function(){
	var indentation = 4;//Change this to vary the indentation

	var pre = document.querySelector(&#039;body pre:only-child&#039;);
	if(!pre) return; //Don&#039;t do anything if this don&#039;t seem to be a json only document
	try{
		pre.innerHTML = JSON.stringify(JSON.parse(pre.innerHTML), null,indentation);
	}
	catch(e){
		console.log(e);
	}
})();
</pre>
<p>
Either save the sourcecode directly from here, or get it from <a href="http://userscripts.org/scripts/show/112472">userscripts.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://martinhansen.no/2011/09/08/format-your-json-with-userscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Text selection mode off in opera</title>
		<link>http://martinhansen.no/2011/06/13/text-selection-mode-off-in-opera/</link>
		<comments>http://martinhansen.no/2011/06/13/text-selection-mode-off-in-opera/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 14:01:55 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://martinhansen.no/?p=249</guid>
		<description><![CDATA[A nice feature in opera is to turn off text selection when dragging on the web page. For instance when using opera on an iPad over remote desktop to your pc. This allows you to drag the page as you would in the native browser on the iPad. To enable this, right click any toolbar [...]]]></description>
			<content:encoded><![CDATA[<p>A nice feature in opera is to turn off text selection when dragging on the web page. For instance when using opera on an iPad over remote desktop to your pc. This allows you to drag the page as you would in the native browser on the iPad.</p>
<p>To enable this, right click any toolbar and press customize &#8211; apperance &#8211; buttons &#8211; browser view. Then drag the button &#8220;text selection on&#8221; to any toolbar of your choice. Then whenever you want to activate the mode, click the button.</p>
]]></content:encoded>
			<wfw:commentRss>http://martinhansen.no/2011/06/13/text-selection-mode-off-in-opera/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting the bearing/heading betweet 2 coordinates</title>
		<link>http://martinhansen.no/2011/04/14/getting-the-bearingheading-betweet-2-coordinates/</link>
		<comments>http://martinhansen.no/2011/04/14/getting-the-bearingheading-betweet-2-coordinates/#comments</comments>
		<pubDate>Thu, 14 Apr 2011 10:33:07 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[bearing]]></category>
		<category><![CDATA[google maps]]></category>
		<category><![CDATA[heading]]></category>
		<category><![CDATA[panorama]]></category>
		<category><![CDATA[streetview]]></category>

		<guid isPermaLink="false">http://martinhansen.no/?p=242</guid>
		<description><![CDATA[Thanks to rjsteward with his answer over at stackoverflow.com I have now a simple function for getting the bearing between to coordinates: The function is meant to be used with google maps, so the parameters are google.maps.LatLng objects function getBearing (from, to) { var lat1 = from.lat() * Math.PI / 180; var lon1 = from.lng(); [...]]]></description>
			<content:encoded><![CDATA[<p>Thanks to <a href="http://stackoverflow.com/users/76639/rjsteward">rjsteward</a> with his <a href="http://stackoverflow.com/questions/1971585/mapping-math-and-javascript">answer</a> over at stackoverflow.com I have now a simple function for getting the bearing between to coordinates:</p>
<p>The function is meant to be used with google maps, so the parameters are google.maps.LatLng objects </p>
<pre class="brush: javascript">
function getBearing (from, to) {
            var lat1 = from.lat() * Math.PI / 180;
            var lon1 = from.lng();

            var lat2 = to.lat() * Math.PI / 180;
            var lon2 = to.lng();

            var dLon = (lon2 - lon1) * Math.PI / 180;
            var y = Math.sin(dLon) * Math.cos(lat2);
            var x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);

            var bearing = Math.atan2(y, x) * 180 / Math.PI;
            if (bearing &lt; 0) {
                bearing = bearing + 360;
            }
            return bearing;
        }
</pre>
<p>This allows you to easily set the heading in the streetview, between your &#8220;streetviewman&#8221; and the actual position you are trying to see. So that you are looking to that direction from the street.</p>
<pre class="brush: javascript">
map.getStreetView().setPov({ heading: getBearing(streetViewManPosition, myLocationPosition), zoom: 1, pitch: 0 });
</pre>
<p>Resulting in something like this:</p>
<p><a href="http://martinhansen.no/wp-content/uploads/streetview-heading.png"><img src="http://martinhansen.no/wp-content/uploads/streetview-heading.png" alt="" title="streetview heading" width="475" height="255" class="aligncenter size-full wp-image-245" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://martinhansen.no/2011/04/14/getting-the-bearingheading-betweet-2-coordinates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to return clean JSON from EF object with data attributes</title>
		<link>http://martinhansen.no/2011/04/04/how-to-return-clean-json-from-ef-object-with-data-attributes/</link>
		<comments>http://martinhansen.no/2011/04/04/how-to-return-clean-json-from-ef-object-with-data-attributes/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 16:25:16 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[attributes]]></category>
		<category><![CDATA[entity framework]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://martinhansen.no/?p=232</guid>
		<description><![CDATA[Returning JSON is very simple in ASP.NET MVC: return Json(Person, JsonRequestBehavior.AllowGet); (Person is an Entity Framework object) The problem with this is if Person have related objects, like e.g. Friends. Then the serializer will throw an error because it will get a circular reference when tying to serialize Person. So one common solution is to [...]]]></description>
			<content:encoded><![CDATA[<p>Returning JSON is very simple in ASP.NET MVC:</p>
<pre class="brush: c#">
 return Json(Person,  JsonRequestBehavior.AllowGet);
</pre>
<p>(Person is an Entity Framework object)</p>
<p>The problem with this is if Person have related objects, like e.g. Friends. Then the serializer will throw an error because it will get a circular reference when tying to serialize Person.</p>
<p>So one common solution is to just create a new anonymous object on the fly like so:</p>
<pre class="brush: c#">
 return Json(new {Name = Person.Name, Age = Person.Age},  JsonRequestBehavior.AllowGet);
</pre>
<p>A second solution is to create a <a href="http://en.wikipedia.org/wiki/Plain_Old_CLR_Object">Poco</a> class that you fill with data from the entity object.</p>
<p>A third solution is to &#8220;tag&#8221; your model members with Attributes. But you cannot do this with an EF class, since the codefile is autogenerated, and if you change the code then your changes will be overwritten the next time you use the designer.</p>
<p>The following code demonstrates how to make this work anyway. </p>
<pre class="brush: c#">
public class PocoAttribute : System.Attribute
{
}

[MetadataType(typeof(PersonMeta))]
public partial class Person
{
}

public class PersonMeta
{
    [Poco]
    public string Name { get; set; }
    [Poco]
    public int Age { get; set; }
}
</pre>
<p>This is the same approach used to add data annotations to members in your EF objects. (See <a href="http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs">http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs</a>)</p>
<p>Now we need a way to automatically return a JsonResult with only the properties we have given our attribute to. For this we can create an extension method:</p>
<pre class="brush: c#">
public static Dictionary&lt;string, object&gt; GetPocoDictionary(this EntityObject obj)
{
    PropertyInfo[] properties = obj.GetType().GetProperties();
    var pocoMembers = properties.Where(d=&gt; d.GetCustomAttributes(typeof(PocoAttribute), true).Count() &gt;= 1).ToArray();

    var metadataAttribute = (MetadataTypeAttribute)obj.GetType()
    .GetCustomAttributes(typeof(MetadataTypeAttribute), true).FirstOrDefault();

    var metaClassMembers = metadataAttribute.MetadataClassType.GetProperties()
    .Where(d=&gt; d.GetCustomAttributes(typeof(PocoAttribute), true).Count() &gt;= 1).ToArray();

    var combinedPoco = pocoMembers.Union(metaClassMembers);
    var allMembers = from propall in properties
                     join combined in combinedPoco on propall.Name equals combined.Name
                     select propall;

    var dict = new Dictionary&lt;string, object&gt;();
    foreach (var item in allMembers)
    {
        dict.Add(item.Name, item.GetValue(obj, null));
    }
    return dict;
}
</pre>
<p>This will take any EF object and return a dictionary with only the members that we have given the attribute to.<br />
Now to use it:</p>
<pre class="brush: c#">
 return Json(Person.GetPocoDictionary(),  JsonRequestBehavior.AllowGet);
</pre>
<p>That&#8217;s it!</p>
]]></content:encoded>
			<wfw:commentRss>http://martinhansen.no/2011/04/04/how-to-return-clean-json-from-ef-object-with-data-attributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery checkbox/radiobutton dependence plugin</title>
		<link>http://martinhansen.no/2011/03/29/jquery-dependance-plugin/</link>
		<comments>http://martinhansen.no/2011/03/29/jquery-dependance-plugin/#comments</comments>
		<pubDate>Tue, 29 Mar 2011 13:44:57 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://martinhansen.no/?p=208</guid>
		<description><![CDATA[I have made a small jQuery plugin that enables you to have checkboxes and radiobuttons that depends on each other in a nested way. The usage is very simple, just take a collection of radios or checkboxes and define an element they depend on. If you check an element that depends on something, the parent [...]]]></description>
			<content:encoded><![CDATA[<p>I have made a small jQuery plugin that enables you to have checkboxes and radiobuttons that depends on each other in a nested way.</p>
<p>The usage is very simple, just take a collection of radios or checkboxes and define an element they depend on. If you check an element that depends on something, the parent will be checked as well.</p>
<p>In the same way, if you uncheck a parent element that have children, then the children will be unchecked as well.</p>
<p>The plugin is designed to also work with jQuery UI buttons</p>
<pre class="brush: javascript">
$(&#039;#fruitlist&gt;li&gt;input&#039;).dependsOn(&#039;#fruits&#039;);
$(&#039;#citruslist input&#039;).dependsOn(&#039;#citrus&#039;);
</pre>
<pre class="brush: javascript">
&lt;ul&gt;
	&lt;li&gt;&lt;input type=&quot;radio&quot; name=&quot;products&quot; value=&quot;books&quot;&gt;books&lt;/li&gt;
	&lt;li&gt;&lt;input type=&quot;radio&quot; name=&quot;products&quot; id=&quot;fruits&quot; value=&quot;fruits&quot;&gt;fruits
		&lt;ul id=&quot;fruitlist&quot;&gt;
			&lt;li&gt;&lt;input type=&quot;checkbox&quot;&gt;apples&lt;/li&gt;
			&lt;li&gt;&lt;input type=&quot;checkbox&quot; id=&quot;citrus&quot;&gt;citrus fruits
				&lt;ul id=&quot;citruslist&quot;&gt;
					&lt;li&gt;&lt;input type=&quot;radio&quot; name=&quot;citrus&quot;&gt;lemons&lt;/li&gt;
					&lt;li&gt;&lt;input type=&quot;radio&quot; name=&quot;citrus&quot;&gt;oranges&lt;/li&gt;
				&lt;/ul&gt;
			&lt;/li&gt;
		&lt;/ul&gt;
	&lt;/li&gt;
	&lt;li&gt;&lt;input type=&quot;radio&quot; name=&quot;products&quot; value=&quot;pens&quot;&gt;pens&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p><strong>Demo:</strong></p>
<style>
ul{
list-style:none;
}
</style>
<p><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js" type="text/javascript"></script><br />
<script src="http://martinhansen.no/wp-content/uploads/dependancy1.js" type="text/javascript"></script> </p>
<p><script type="text/javascript">
   $(function(){
       $('#fruitlist>li>input').dependsOn('#fruits');
       $('#citruslist input').dependsOn('#citrus');
    });
</script></p>
<ul>
<li>
<input type="radio" name="products" value="books">books</li>
<li>
<input type="radio" name="products" id="fruits" value="fruits">fruits</p>
<ul id="fruitlist">
<li>
<input type="checkbox">apples</li>
<li>
<input type="checkbox" id="citrus">citrus fruits</p>
<ul id="citruslist">
<li>
<input type="radio" name="citrus">lemons</li>
<li>
<input type="radio" name="citrus">oranges</li>
</ul>
</li>
</ul>
</li>
<li>
<input type="radio" name="products" value="pens">pens</li>
</ul>
<p>Here is the code:</p>
<pre class="brush: javascript">
/* jQuery checkbox/radiobutton dependance plugin
* By Martin Hansen http://martinhansen.no
* MIT Licensed.
*/
(function($) {
  $.fn.dependsOn = function(parent) {
    // build main options before element iteration
    if(parent === undefined){ console.log(&#039;Parent is required&#039;); return;   }        

    var opts = {parent: parent, value: null};

     //If parent is a radiobutton part of a group, make the group the parent
    if($(opts.parent).attr(&#039;type&#039;) == &#039;radio&#039;){
      opts.origparent  = $(opts.parent);
      opts.parent = &#039;input[name=&#039; +$(opts.parent).attr(&#039;name&#039;) +&#039;]&#039;;
    }  

    return this.each(function() {
    var caller = $(this);
    $.data(this, &#039;dependsOnOptions&#039;, opts);//Store the dependency options

    caller.bind(&#039;click iterate&#039;, function(event){
      var parent = (opts.origparent) ? opts.origparent : $(opts.parent);
      parent.attr(&#039;checked&#039;, true).trigger(&#039;iterate&#039;, [&#039;Iterate&#039;, &#039;Event&#039;]);
      if (jQuery.ui)parent.button(&#039;refresh&#039;); //If jquery ui is loaded try to refesh button
    });              

    $(opts.parent).each(function(i){
      var pp = $(this);
      //Do first time checks
      var checked = pp.attr(&#039;checked&#039;);
      if(checked){
        $.fn.dependsOn.check(pp, caller, opts);
      }
      //bind for change
      pp.change(function(event){
        $.fn.dependsOn.check($(this), caller, opts);
      });
    });

   });
  };     

   $.fn.dependsOn.check = function(parent, child, opts){
    if (!parent.is(&#039;:checked&#039;) || !$(opts.origparent).is(&#039;:checked&#039;)) {
      child.attr(&#039;checked&#039;, false).change(); //uncheck the checked child, and trigger the change event so that any potential grandchildren also gets updated
      if (jQuery.ui)parent.button(&#039;refresh&#039;); //If jquery ui is loaded try to refesh button
    }
   };
})(jQuery);
</pre>
<p>On github: <a href="https://github.com/mokkabonna/jQuery-dependency">https://github.com/mokkabonna/jQuery-dependency</a></p>
<p>Edit:updated the code to use .is(&#8216;:checked&#8217;) instead of .attr(&#8216;checked&#8217;), as jQuery 1.6.3 returns &#8216;checked&#8217; instead of true. Thanks to &#8220;all&#8221; in the comments for making me aware of if.</p>
]]></content:encoded>
			<wfw:commentRss>http://martinhansen.no/2011/03/29/jquery-dependance-plugin/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Network problems with DNS symptoms</title>
		<link>http://martinhansen.no/2010/03/14/network-problems-with-dns-symptoms/</link>
		<comments>http://martinhansen.no/2010/03/14/network-problems-with-dns-symptoms/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 19:16:09 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://martinhansen.no/?p=185</guid>
		<description><![CDATA[If you are having what seems to be DNS problems in your network, but others seem to connect without problems it might not be DNS problems at all, but something to do with your MTU setting, read more here. This solved a problem I&#8217;ve had for a long time on a friends network.]]></description>
			<content:encoded><![CDATA[<p>If you are having what seems to be DNS problems in your network, but others seem to connect without problems it might not be DNS problems at all, but something to do with your MTU setting, read more <a href="http://groups.google.com/group/blogger-help/web/how-to-check-the-mtu-setting-on-your-computer">here</a>. This solved a problem I&#8217;ve had for a long time on a friends network.</p>
]]></content:encoded>
			<wfw:commentRss>http://martinhansen.no/2010/03/14/network-problems-with-dns-symptoms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

