<?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>cakebaker &#187; jquery</title>
	<atom:link href="http://cakebaker.42dh.com/tags/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://cakebaker.42dh.com</link>
	<description>baking cakes with CakePHP</description>
	<lastBuildDate>Tue, 20 Dec 2011 15:29:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Navigation with the &#8220;j&#8221; and &#8220;k&#8221; keys</title>
		<link>http://cakebaker.42dh.com/2010/12/04/navigation-with-the-j-and-k-keys/</link>
		<comments>http://cakebaker.42dh.com/2010/12/04/navigation-with-the-j-and-k-keys/#comments</comments>
		<pubDate>Sat, 04 Dec 2010 09:33:19 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=1456</guid>
		<description><![CDATA[If you are using Vim you already know the meaning of the &#8220;j&#8221; and &#8220;k&#8221; keys: they navigate one line downwards resp. upwards. Some websites like The Big Picture adopted this functionality to provide an easy way to navigate, in the case of The Big Picture to jump from photo to photo. As I wanted [...]]]></description>
			<content:encoded><![CDATA[<p>If you are using <a href="http://www.vim.org/">Vim</a> you already know the meaning of the &#8220;j&#8221; and &#8220;k&#8221; keys: they navigate one line downwards resp. upwards. Some websites like <a href="http://www.boston.com/bigpicture/">The Big Picture</a> adopted this functionality to provide an easy way to navigate, in the case of The Big Picture to jump from photo to photo.</p>
<p>As I wanted to use the same functionality and didn&#8217;t find an existing solution I wrote a simple <a href="http://jquery.com/">jQuery</a> plugin for this purpose: <a href="https://github.com/cakebaker/jquery-jknavigable">jquery-jknavigable</a>. Its usage is pretty simple, to make the posts of a blog navigable with the &#8220;j&#8221; and &#8220;k&#8221; keys you would use the following code:</p>
<pre>
<code>$('.post').jknavigable();</code>
</pre>
<p>By default the active element is marked with the class &#8220;active&#8221; so you can style it differently. If necessary, you can specify your own class name:</p>
<pre>
<code>$('.post').jknavigable({'activeClass': 'someClass'});</code>
</pre>
<p>That&#8217;s it. Feedback is, as always, welcome.</p>
<p>PS: a good starting point for writing your own plugin is jQuery&#8217;s <a href="http://docs.jquery.com/Plugins/Authoring">Plugin Authoring page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2010/12/04/navigation-with-the-j-and-k-keys/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Enabling submit button if text entered</title>
		<link>http://cakebaker.42dh.com/2009/01/13/enabling-submit-button-if-text-entered/</link>
		<comments>http://cakebaker.42dh.com/2009/01/13/enabling-submit-button-if-text-entered/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 17:11:30 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[jquery]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=1061</guid>
		<description><![CDATA[While looking at some jQuery code I wrote a while ago I noticed the following snippet: $('#username').keyup(function() { if ($('#username').val() != '') { $('#submit').removeAttr('disabled'); } else { $('#submit').attr('disabled', 'disabled'); } }); It ensures that the submit button is only enabled if the &#8220;username&#8221; input field is not empty (you can see such a behavior on [...]]]></description>
			<content:encoded><![CDATA[<p>While looking at some <a href="http://jquery.com">jQuery</a> code I wrote a while ago I noticed the following snippet:</p>
<pre>
<code>$('#username').keyup(function() {
    if ($('#username').val() != '') {
        $('#submit').removeAttr('disabled');
    } else {
        $('#submit').attr('disabled', 'disabled');
    }
});</code>
</pre>
<p>It ensures that the submit button is only enabled if the &#8220;username&#8221; input field is not empty (you can see such a behavior on twitter for example). </p>
<p>However, the snippet from above can be rewritten in a shorter and more elegant way:</p>
<pre>
<code>$('#username').keyup(function() {
    $('#submit').attr('disabled', !$('#username').val()); 
});</code>
</pre>
<p>Maybe this is helpful for someone out there ;-)</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2009/01/13/enabling-submit-button-if-text-entered/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Edit in place with JQuery and CakePHP</title>
		<link>http://cakebaker.42dh.com/2008/02/24/edit-in-place-with-jquery-and-cakephp/</link>
		<comments>http://cakebaker.42dh.com/2008/02/24/edit-in-place-with-jquery-and-cakephp/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 10:07:41 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/2008/02/24/edit-in-place-with-jquery-and-cakephp/</guid>
		<description><![CDATA[Sometimes it is quite handy if the users of an application can simply click on some text to edit it. Adding such a feature to your application is not very difficult as you will see. At first we have to get the Jeditable plugin (plus the JQuery library if you don&#8217;t have it already) and [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes it is quite handy if the users of an application can simply click on some text to edit it. Adding such a feature to your application is not very difficult as you will see.</p>
<p>At first we have to get the <a href="http://www.appelsiini.net/projects/jeditable">Jeditable</a> plugin (plus the <a href="http://jquery.com/">JQuery</a> library if you don&#8217;t have it already) and place it in app/webroot/js. In this folder we also create a js file which will contain the Javascript code we have to write (I will name this file example.js). </p>
<p>To use the Javascript files, we have to include them in the layout (or in the view, see <a href="http://cakebaker.42dh.com/2007/02/21/referencing-javascript-files/">&#8220;Referencing Javascript files&#8221;</a> for more information):</p>
<pre>
// app/views/layouts/default.ctp
&lt;?php echo $javascript-&gt;link(array('jquery-1.2.3', 'jquery.jeditable', 'example')); ?&gt;
</pre>
<p>Don&#8217;t forget to add the Javascript helper to the $helpers array of your (App)Controller.</p>
<p>The next step is to write the &#8220;normal&#8221; functionality of the application, in this example we simply list the titles of all posts. The controller (app/controllers/posts_controller.php):</p>
<pre>
class PostsController extends AppController {

    public function index() {
        $this-&gt;set('posts', $this-&gt;Post-&gt;find('all'));
    }
}
</pre>
<p>And the view (app/views/posts/index.ctp):</p>
<pre>
&lt;?php foreach ($posts as $post): ?&gt;
    &lt;div class="post" id="&lt;?php echo $post['Post']['id']; ?&gt;"&gt;&lt;?php echo $post['Post']['title']; ?&gt;&lt;/div&gt;
&lt;?php endforeach; ?&gt;
</pre>
<p>With that we are ready to add the edit in place functionality. Thanks to the Jeditable plugin this is an easy task: we only have to define the editable element(s), the url to which the changes should be posted, plus some parameters.</p>
<pre>
// app/webroot/js/example.js
$(function() {
    $('.post').editable('/posts/updateTitle', {
         id        : 'data[Post][id]',
         name      : 'data[Post][title]',
         type      : 'text',
         cancel    : 'Cancel',
         submit    : 'Save',
         tooltip   : 'Click to edit the title'
    });
});
</pre>
<p>I think this code is self-explanatory (if not, please leave a comment), so we can go on to implement the updateTitle action. We also add the RequestHandler component to our controller, so that the response of the updateTitle action automatically uses the ajax layout:</p>
<pre>
class PostsController extends AppController {
    public $components = array('RequestHandler');

    public function index() {
        $this-&gt;set('posts', $this-&gt;Post-&gt;find('all'));
    }

    public function updateTitle() {
        if ($this-&gt;data) {
            App::import('Core', 'sanitize');
            $title = Sanitize::clean($this-&gt;data['Post']['title']);

            $this-&gt;Post-&gt;id = $this-&gt;data['Post']['id'];
            $this-&gt;Post-&gt;saveField('title', $title);
            $this-&gt;set('posttitle', $title);
        }
    }
}
</pre>
<p>Last, but not least, we have to create a simple view for the updateTitle action (app/views/posts/update_title.ctp):</p>
<pre>
&lt;?php echo $posttitle;?&gt;
</pre>
<p>That&#8217;s it, our application now provides edit in place functionality. Happy baking :)</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2008/02/24/edit-in-place-with-jquery-and-cakephp/feed/</wfw:commentRss>
		<slash:comments>101</slash:comments>
		</item>
		<item>
		<title>MVC with Javascript</title>
		<link>http://cakebaker.42dh.com/2007/03/17/mvc-with-javascript/</link>
		<comments>http://cakebaker.42dh.com/2007/03/17/mvc-with-javascript/#comments</comments>
		<pubDate>Sat, 17 Mar 2007 14:38:03 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/2007/03/17/mvc-with-javascript/</guid>
		<description><![CDATA[Javascript was one of those things I tried to avoid up to now. I don&#8217;t know why, but somehow I disliked it (probably because it was a pain to use it in the early days of Javascript) ;-) Sure, I used a bit of Ajax here and there, but thanks to the CakePHP Ajax helper [...]]]></description>
			<content:encoded><![CDATA[<p>Javascript was one of those things I tried to avoid up to now. I don&#8217;t know why, but somehow I disliked it (probably because it was a pain to use it in the early days of Javascript) ;-)  Sure, I used a bit of Ajax here and there, but thanks to the CakePHP Ajax helper I didn&#8217;t had to touch the Javascript. </p>
<p>Lately, I decided to do an Ajax application and so I had to learn Javascript. To make my life easier, I decided to use the <a href="http://jquery.com">JQuery framework</a>. I have chosen JQuery over <a href="http://www.prototypejs.org">Prototype</a> because I heard many good things about JQuery, and the API documentation was (and still is) more intuitive. </p>
<p>Of course, my first attempt ended in a total mess. The result looked more like spaghetti than code ;-)  That&#8217;s when I recalled an <a href="http://cakebakery.de/2006/08/17/waschmittel-in-der-kuche/">article</a> (in German) about a Javascript MVC framework called <a href="http://jamal.moagil.de/">Jamal</a>, developed by <a href="http://teemow.com">Timo Derstappen</a>. From the Jamal website:</p>
<blockquote><p>
The MVC concept is easy to adopt for javascript </p>
<ul>
<li>Controller: Interaction with the user interface (events) </li>
<li>Model: Business Logic and AJAX calls </li>
<li>View: DOM, CSS modifications </li>
</ul>
</blockquote>
<p>That makes sense. So I had a closer look at Jamal. It looked nice, but it bothered me that you would have to use a CSS class &#8220;jamal&#8221; in the HTML code to make it work. So I wrote my own implementation called &#8220;jscake&#8221; which avoids that. You can find the first version in the <a href="http://cakebaker.42dh.com/downloads">downloads section</a>.</p>
<p>Let&#8217;s create a simple &#8220;Hello world&#8221; example with jscake. Our example (CakePHP) view will contain a link and a div to show the messages: </p>
<pre>
&lt;a href=""&gt;click&lt;/a&gt;
&lt;div id="messages"&gt;&lt;/div&gt;
</pre>
<p>First we create our model. All models are in the &#8220;$m&#8221; namespace (please correct me if that is the wrong term), which is a shortcut for &#8220;jscake.models&#8221;. So our model is called &#8220;$m.Example&#8221;. The model contains one function to return the text which should be displayed:</p>
<pre>
// app/webroot/js/models/example.js
$m.Example = {

    getText: function() {
        return "hello world";
    }
};
</pre>
<p>The view is similar to the model, it contains only one function, too. In the function the element with the id &#8220;messages&#8221; is located and the text appended to that element. Similar to the models the views are in the &#8220;$v&#8221; namespace.</p>
<pre>
// app/webroot/js/views/examples.js
$v.Examples = {

    showMessage: function(message) {
        $('#messages').append(message);
    }
};
</pre>
<p>Last, but not least, we have to create the controller. The index function is automatically called when the JQuery ready event occurs. In this function we simply say that when someone clicks on the link, the other function of the controller (sayHelloWorld) should be executed.</p>
<pre>
//app/webroot/js/controllers/examples_controller.js
$c.ExamplesController = {

    index: function() {
        $('a').click(this.sayHelloWorld);
    },

    sayHelloWorld: function() {
        $v.Examples.showMessage($m.Example.getText());

        return false;
    }
};
</pre>
<p>To make our example work we have to include all needed Javascript files in our view, with CakePHP 1.2 it looks like:</p>
<pre>
&lt;?php $javascript-&gt;link(array('jquery-1.1.2', 'jscake', 'controllers/examples_controller',
'models/example', 'views/examples', 'start'), false); ?&gt;
&lt;a href=""&gt;click&lt;/a&gt;
&lt;div id="messages"&gt;&lt;/div&gt;
</pre>
<p>Have fun with jscake! Feedback is welcome :)</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2007/03/17/mvc-with-javascript/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>

