<?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>HiddenTao</title>
	<atom:link href="http://www.hiddentao.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hiddentao.com</link>
	<description>software, websites, mobile, technology</description>
	<lastBuildDate>Fri, 27 Jan 2012 13:11:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Processing long-running Django tasks using Celery + RabbitMQ + Supervisord + Monit</title>
		<link>http://www.hiddentao.com/archives/2012/01/27/processing-long-running-django-tasks-using-celery-rabbitmq-supervisord-monit/</link>
		<comments>http://www.hiddentao.com/archives/2012/01/27/processing-long-running-django-tasks-using-celery-rabbitmq-supervisord-monit/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 13:11:27 +0000</pubDate>
		<dc:creator>ram</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Celery]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[RabbitMQ]]></category>

		<guid isPermaLink="false">http://www.hiddentao.com/?p=1356</guid>
		<description><![CDATA[For a recent project I&#8217;m working on we had a requirement to be able to process long-running tasks in Django triggered by user interaction on the front-end. For instance, the user would click a button on the web page in order to trigger the back-end to, for example, build a CSV file containing a subset [...]]]></description>
			<content:encoded><![CDATA[<p>For a recent project I&#8217;m working on we had a requirement to be able to process long-running tasks in Django triggered by user interaction on the front-end. For instance, the user would click a button on the web page in order to trigger the back-end to, for example, build a CSV file containing a subset of the data in the database. In the browser a little popup window would get displayed, showing the progress of the task in the back-end. Once the task got completed in the back-end the user would be notified in the front-end of its completion and provided a link to download the final output (e.g. the built CSV file).
<span id="more-1356"></span></p>

<p>For a requirement like this, you don&#8217;t really want to be performing such a long-running task as part of the standard Django <em>request -> response</em> cycle since 1) the Django server is single-threaded, and 2) even if you&#8217;re using <a href="http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango" class="link-external">WSGI</a> to run your Django app behind Apache or a similar server you&#8217;re not really going to want one of your Apache worker processes being tied up with a single client request for long periods of time as this would adversely impact other incoming client requests. Thus, any such long-running task should be performed in a process which then informs the main Django app once it&#8217;s done. <a href="http://celeryproject.org/" class="link-external">Celery</a> is a readily-available such system (a <em>task-queue</em> to be precise) which enables this and it is easy to integrate into Django using <a href="http://pypi.python.org/pypi/django-celery" class="link-external">django-celery</a>.</p>

<h2>Setting up Django Celery</h2>

<p>Setting up Django Celery has already been <a href="http://django-celery.readthedocs.org/en/latest/getting-started/first-steps-with-django.html" class="link-external">documented</a> <a href="http://pypi.python.org/pypi/django-celery" class="link-external">elsewhere</a> so I&#8217;ll simply list the settings I used to get things working (<em>Note: I&#8217;m assuming that you&#8217;re running a Debian-based Linux system</em>). First of all I installed <a href="">RabbitMQ</a> to use the message queue system:</p>

<div><pre class="brush: bash; title: ; notranslate">
$ sudo apt-get install rabbitmq-server
</pre></div>

<p>Then I added a <code>vhost</code> and username and password for my Django app to RabbitMQ:</p>

<div><pre class="brush: bash; title: ; notranslate">
$ sudo rabbitmqctl add_user myapp myapp
$ sudo rabbitmqctl add_vhost myapp
$ sudo rabbitmqctl set_permissions -p myapp myapp &quot;.*&quot; &quot;.*&quot; &quot;.*&quot;
</pre></div>

<p>Then in my <code>celeryconfig.py</code> I set the following:</p>

<div><pre class="brush: python; title: ; notranslate">
BROKER_HOST = &quot;127.0.0.1&quot;
BROKER_PORT = 5672   # default RabbitMQ listening port
BROKER_USER = &quot;myapp&quot;
BROKER_PASSWORD = &quot;myapp&quot;
BROKER_VHOST = &quot;myapp&quot;
CELERY_BACKEND = &quot;amqp&quot;  # telling Celery to report the results back to RabbitMQ
CELERY_RESULT_DBURI = &quot;&quot;
</pre></div>

<p>To test that my setup was correct I ran:</p>

<div><pre class="brush: bash; title: ; notranslate">
$ ./manage.py celeryd -l INFO

[2012-01-27 12:29:01,344: WARNING/MainProcess]  
/home/ram/dev/myapp/virtualenv/lib/python2.7/site-packages/djcelery/loaders.py:86: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
  warnings.warn(&quot;Using settings.DEBUG leads to a memory leak, never &quot;
[2012-01-27 12:29:01,344: WARNING/MainProcess]  

 -------------- celery@RamLaptop2 v2.4.6
---- **** -----
--- * ***  * -- [Configuration]
-- * - **** ---   . broker:      amqp://guest@localhost:5672//
- ** ----------   . loader:      djcelery.loaders.DjangoLoader
- ** ----------   . logfile:     [stderr]@INFO
- ** ----------   . concurrency: 8
- ** ----------   . events:      OFF
- *** --- * ---   . beat:        OFF
-- ******* ----
--- ***** ----- [Queues]
 --------------   . celery:      exchange:celery (direct) binding:celery
                  
[Tasks]
  . REPORT_CREATE

[2012-01-27 12:29:01,399: INFO/PoolWorker-1] child process calling self.run()
[2012-01-27 12:29:01,401: INFO/PoolWorker-2] child process calling self.run()
[2012-01-27 12:29:01,403: INFO/PoolWorker-3] child process calling self.run()
[2012-01-27 12:29:01,405: INFO/PoolWorker-4] child process calling self.run()
[2012-01-27 12:29:01,406: INFO/PoolWorker-5] child process calling self.run()
[2012-01-27 12:29:01,408: INFO/PoolWorker-6] child process calling self.run()
[2012-01-27 12:29:01,409: INFO/PoolWorker-7] child process calling self.run()
[2012-01-27 12:29:01,410: INFO/PoolWorker-8] child process calling self.run()
[2012-01-27 12:29:01,411: WARNING/MainProcess] celery@RamLaptop2 has started.
</pre></div>

<p>At this point if you&#8217;re not familiar with writing Celery tasks then check out their tutorial on <a href="http://docs.celeryproject.org/en/latest/tutorials/clickcounter.html" class="link-external">how to write Celery tasks</a> for use by Celery daemon workers started above.</p>

<h2>Deploying to production using Supervisord</h2>

<p>In the production environment we need a reliable way of running the Celery daemon processes. Enter <a href="http://supervisord.org/" class="link-external">Supervisord</a>. Essentially it&#8217;s a processes which in turn launches other processes you tell it to launch, and then monitors those child processes, restarting them if they die, etc. Here is what I did to set it up:</p>

<div><pre class="brush: bash; title: ; notranslate">
$ sudo easy_install supervisor
</pre></div>

<p>I created <code>/etc/supervisord</code> to hold all the configuration info:</p>

<div><pre class="brush: bash; title: ; notranslate">
$ sudo mkdir /etc/supervisord
[/code lang=&quot;bash&quot;]

I then edited `/etc/supervisord/supervisord.conf`:

1
[unix_http_server]
file=/tmp/supervisor.sock   ; (the path to the socket file)

[supervisord]
logfile=/var/log/supervisord/main.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB        ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10           ; (num of main logfile rotation backups;default 10)
loglevel=info                ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false               ; (start in foreground if true;default false)
minfds=1024                  ; (min. avail startup file descriptors;default 1024)
minprocs=200                 ; (min. avail process descriptors;default 200)
childlogdir=/var/log/supervisord            ; ('AUTO' child log dir, default $TEMP)

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket

[include]
files = /etc/supervisord/conf.d/*.conf
</pre></div>

<p>I created a folder called <code>conf.d</code> inside <code>/etc/supervisord</code> and created a file called <code>myapp-celery.conf</code> inside that:</p>

<div><pre class="brush: plain; title: ; notranslate">
[program:myapp-celery]
command=/home/ram/dev/myapp/virtualenv/bin/python /home/ram/dev/myapp/manage.py celeryd --loglevel=INFO
environment=PYTHONPATH=/home/ram/dev/myapp
directory=/home/ram/dev/myapp
user=www-data
numprocs=1
stdout_logfile=/var/log/celeryd/myapp.log
stderr_logfile=/var/log/celeryd/myapp.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs = 600
priority=998
</pre></div>

<p><strong>Note: I'm running my Django app inside a virtual environment, which is why I specify the path to the Python interpreter in the above file.</strong></p>

<p>I created the <code>/var/log/supervisord</code> and <code>/var/log/celeryd</code> log folders and setup the appropriate permissions on them to enable logging.</p>

<p>To run Supervisord I did the following:</p>

<div><pre class="brush: bash; title: ; notranslate">
$ supervisord -c /etc/supervisord/supervisord.conf
</pre></div>

<p>I checked that my Celery workers were active:</p>

<div><pre class="brush: bash; title: ; notranslate">
$ ps aux | grep celeryd
...
www-data  26655  0.5  0.3 210000 36248 ?        Sl   12:49   0:02 /home/ram/dev/myapp/virtualenv/bin/python /home/ram/dev/myapp/manage.py celeryd --loglevel=INFO
www-data  26656  0.5  0.3 210012 36232 ?        Sl   12:49   0:02 /home/ram/dev/myapp/virtualenv/bin/python /home/ram/dev/myapp/manage.py celeryd --loglevel=INFO
www-data  26671  0.0  0.2 103364 33012 ?        S    12:49   0:00 /home/ram/dev/myapp/virtualenv/bin/python /home/ram/dev/myapp/manage.py celeryd --loglevel=INFO
...
</pre></div>

<p>To shut it down I did:</p>

<div><pre class="brush: bash; title: ; notranslate">
$ supervisorctl -c /etc/supervisord/supervisord.conf shutdown
</pre></div>

<h2>Monitoring Supervisord</h2>

<p>Supervisord will monitor our Celery workers and ensure they stay alive, but how will we ensure that Supervisord itself stays alive? Enter <a href="http://mmonit.com/monit/" class="link-external">Monit</a>. Monit does essentially the same thing as Supervisord except that it monitors child processes through pid files and other checks (e.g. checking that a web page loads in order to verify that Apache is running) and doesn't directly own them (unlike Supervisord). Monit also installs itself as an <code>init.d</code> script which gets launched at server boot time. It comes with a web interface (with optional authentication) which lets you easily start, stop and restart any services it is monitoring along with providing memory and CPU usage statistics.</p>

<p>I installed Monit:</p>

<div><pre class="brush: bash; title: ; notranslate">
$ sudo apt-get install monit
</pre></div>

<p>I edited <code>/etc/monit/monitrc</code> and uncommented the lines relating to the web interface:</p>

<div><pre class="brush: plain; title: ; notranslate">
set httpd port 2812 and
#     use address localhost  # only accept connection from localhost
#     allow localhost        # allow localhost to connect to the server and
#     allow admin:monit      # require user 'admin' with password 'monit'
     allow @monit           # allow users of group 'monit' to connect (rw)
#     allow @users readonly  # allow users of group 'users' to connect readonly
</pre></div>

<p><strong>Note: I configured it above such that it accepts authenticated connections from anywhere, whereby only local Linux users who belongs to the <code>monit</code> group can gain access.</strong>.</p>

<p>Then I created <code>/etc/monit/conf.d/supervisord.monit</code>:</p>

<div><pre class="brush: plain; title: ; notranslate">
 check process supervisord with pidfile /tmp/supervisord.pid
   group supervisord
   start program = &quot;/usr/local/bin/supervisord -c /etc/supervisord/supervisord.conf&quot;
   stop  program = &quot;/usr/local/bin/supervisorctl -c /etc/supervisord/supervisord.conf shutdown&quot;
   if 5 restarts within 5 cycles then timeout
</pre></div>

<p>Restart Monit:</p>

<div><pre class="brush: bash; title: ; notranslate">
$ sudo /etc/init.d/monit restart
</pre></div>

<p>All done! I then created a <code>monit</code> Linux group and added myself to it. From then on I was able to visit <code>http://localhost:2812</code> to see the status of the <code>supervisord</code> process and control it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hiddentao.com/archives/2012/01/27/processing-long-running-django-tasks-using-celery-rabbitmq-supervisord-monit/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Squel.js &#8211; lightweight Javascript library for building SQL query strings in node.js or the browser</title>
		<link>http://www.hiddentao.com/archives/2011/12/23/squel-js-lightweight-javascript-library-for-building-sql-query-strings-in-node-js-or-the-browser/</link>
		<comments>http://www.hiddentao.com/archives/2011/12/23/squel-js-lightweight-javascript-library-for-building-sql-query-strings-in-node-js-or-the-browser/#comments</comments>
		<pubDate>Fri, 23 Dec 2011 17:31:48 +0000</pubDate>
		<dc:creator>ram</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[CoffeeScript]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.hiddentao.com/?p=1353</guid>
		<description><![CDATA[I&#8217;d like to announce the immediate availability of Squel.js, a lightweight Javascript library for building SQL query strings. Squel exposes an object-oriented API for building SQL query strings in a server-side or browser-side Javascript environment. It&#8217;s well tested using Vows and is available for forking on github. Full documentation along with in-browser examples are available [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d like to announce the immediate availability of <a href="http://squeljs.org/" class="link-external">Squel.js</a>, a lightweight Javascript library for building SQL query strings. Squel exposes an object-oriented API for building SQL query strings in a server-side or browser-side Javascript environment. It&#8217;s well tested using <a href="http://vowsjs.org/" class="link-external">Vows</a> and is available for forking on <a href="https://github.com/hiddentao/squel" class="link-external">github</a>.
<span id="more-1353"></span></p>

<p>Full documentation along with in-browser examples are available at <a href="http://squeljs.org/" class="link-external">squeljs.org</a>. To get started with using Squel in your node.js program use <code>npm install squel</code>.</p>

<p>Enjoy <img src='http://www.hiddentao.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.hiddentao.com/archives/2011/12/23/squel-js-lightweight-javascript-library-for-building-sql-query-strings-in-node-js-or-the-browser/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Instructing Cakefile to exit with error if a vows test fails</title>
		<link>http://www.hiddentao.com/archives/2011/12/21/instructing-cakefile-to-exit-with-error-if-a-vows-test-fails/</link>
		<comments>http://www.hiddentao.com/archives/2011/12/21/instructing-cakefile-to-exit-with-error-if-a-vows-test-fails/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 17:24:51 +0000</pubDate>
		<dc:creator>ram</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Build]]></category>
		<category><![CDATA[CoffeeScript]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[node.js]]></category>

		<guid isPermaLink="false">http://www.hiddentao.com/?p=1343</guid>
		<description><![CDATA[I&#8217;m using the vows &#8220;behaviour-driven development&#8221; framework to test the squel Javascript library. I have a Cakefile which acts as my build script and inside it there is function which runs all of my tests. I decided to hook it up to the excellent Travis CI automated build system to ensure continuous testing on commits. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using the <a href="http://vowsjs.org/" class="link-external">vows</a> &#8220;behaviour-driven development&#8221; framework to test the <a href="https://github.com/hiddentao/squel" class="link-external">squel</a> Javascript library. I have a Cakefile which acts as my build script and inside it there is function which runs all of my tests. I decided to hook it up to the excellent <a href="http://travis-ci.org/" class="link-external">Travis CI</a> automated build system to ensure continuous testing on commits. One of the problems I had was that <code>cake</code> script exited normally even when one of the vows tests broke.
<span id="more-1343"></span></p>

<p>I came up with this little snippet to ensure that <code>cake</code> exited abnormally (with error code 1) if any of the tests failed:</p>

<div><pre class="brush: jscript; title: ; notranslate">
run_tests = (callback) -&gt;
    options = [
        'test/expression.coffee'
        'test/select.coffee'
        'test/update.coffee'
        'test/delete.coffee'
        'test/insert.coffee'
        '--spec'
    ]
    vows = spawn &quot;#{binpath}/vows&quot;, options
    output = &quot;&quot;
    data_handler = (data) -&gt;
        output =+ data if data
        stream_data_handler data
    vows.stdout.on 'data', data_handler
    vows.stderr.on 'data', data_handler
    vows.on 'exit', (status) -&gt;
        if 0 isnt status or (output and -1 isnt output.indexOf(&quot;✗ Broken&quot;))
            return process.exit(1)
        callback?()
</pre></div>

<p>Essentially it looks for the <code>✗ Broken</code> string in the vows output &#8211; this string only gets output if one or more vows were &#8216;broken&#8217;. Now Travis also picks up on test failures. There might be a more elegant way to do the above &#8211; if you know give me a shout. Otherwise I hope somebody else finds this useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hiddentao.com/archives/2011/12/21/instructing-cakefile-to-exit-with-error-if-a-vows-test-fails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django &#8211; fetching list of all SQL queries executed so far for all requests</title>
		<link>http://www.hiddentao.com/archives/2011/12/18/django-fetching-list-of-all-sql-queries-executed-so-far-for-all-requests/</link>
		<comments>http://www.hiddentao.com/archives/2011/12/18/django-fetching-list-of-all-sql-queries-executed-so-far-for-all-requests/#comments</comments>
		<pubDate>Sun, 18 Dec 2011 10:56:58 +0000</pubDate>
		<dc:creator>ram</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Profiling]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.hiddentao.com/?p=1336</guid>
		<description><![CDATA[As part of a recent Django project I had to provide a way to view all the SQL queries which had been executed through the Django app across all requests, when using the Django test server. Although the debug toolbar that ships with Django shows such query information it doesn&#8217;t work for queries executed as [...]]]></description>
			<content:encoded><![CDATA[<p>As part of a recent Django project I had to provide a way to view all the SQL queries which had been executed through the Django app across all requests, when using the Django test server. Although the debug toolbar that ships with Django shows such query information it doesn&#8217;t work for queries executed as a part of AJAX requests. 
<span id="more-1336"></span></p>

<p>Although the Django test server is single-threaded (and thus all requests execute in the same thread) it does not maintain a global application state which is accessible to each request, as far as I&#8217;m aware. However, because it is single-threaded we can simulate a globally accessible application state through the use of static class member variables.</p>

<p>My snippet below does just this. It uses request-response middleware which kicks in during the response phase to add all SQL queries executed as part of the current request to a static class member list variable. Accessing the data requires calling a separate URL (<code>/profiling</code>) which will then output all the SQL queries so far &#8216;collected&#8217;.</p>

<p>Further improvements to this may include establishing a persistent socket connection to the server through which SQL query strings are received as and when they get executed. And instead of displaying the list of queries on their own page we could inject them into the Debug toolbar, if it&#8217;s enabled. In fact, enhancing the Debug toolbar itself to pick up queries executed as part of AJAX requests would be the best implementation yet.</p>

<p>The snippet (see <a href="http://djangosnippets.org/snippets/2632/" class="link-external">http://djangosnippets.org/snippets/2632/</a>):</p>

<div><pre class="brush: python; title: ; notranslate">

# file: settings.py

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'yourapp.middleware.SqlProfilingMiddleware',
    ...
)



# file: yourapp/midddlware.py

from django.db import connection
import time

class SqlProfilingMiddleware(object):
    Queries = []

    def process_request(self, request):
        return None
    def process_view(self, request, view_func, view_args, view_kwargs):
        return None
    def process_template_response(self, request, response):
        self._add_sql_queries(request)
        return response
    def process_response(self, request, response):
        self._add_sql_queries(request)
        return response
    def process_exception(self, request, exception):
        return None

    def _add_sql_queries(self, request):
        for q in connection.queries:
            q[&quot;time&quot;] = time.time() + float(q[&quot;time&quot;])
            SqlProfilingMiddleware.Queries.insert(0, q)
            # add request info as a separator
        SqlProfilingMiddleware.Queries.insert(0, {&quot;time&quot;: time.time(), &quot;path&quot; : request.path})



# file: yourapp/views.py

from yourapp.middleware import SqlProfilingMiddleware

def profiling(request):
    return render_to_response(&quot;profiling.html&quot;, {&quot;queries&quot;: SqlProfilingMiddleware.Queries})



# file: urls.py

urlpatterns = patterns('',
  (r'^profiling/$', 'yourapp.views.profiling'),
)

</pre></div>
]]></content:encoded>
			<wfw:commentRss>http://www.hiddentao.com/archives/2011/12/18/django-fetching-list-of-all-sql-queries-executed-so-far-for-all-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Primary key, foreign key improvements to Sequelize + Date.js alternatives</title>
		<link>http://www.hiddentao.com/archives/2011/11/18/primary-key-foreign-key-improvements-to-sequelize-date-js-alternatives/</link>
		<comments>http://www.hiddentao.com/archives/2011/11/18/primary-key-foreign-key-improvements-to-sequelize-date-js-alternatives/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 16:54:51 +0000</pubDate>
		<dc:creator>ram</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[node.js]]></category>
		<category><![CDATA[ORM]]></category>

		<guid isPermaLink="false">http://www.hiddentao.com/?p=1332</guid>
		<description><![CDATA[I&#8217;ve created a (https://github.com/sdepold/sequelize/pull/110) for Sequelize which enables one to use primary key fields as foreign keys too, something I find myself doing often with my relational schemas as I don&#8217;t like using the standard id primary key field unless it&#8217;s the most sensible primary key to have. In practice the changes mean you can [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve created a (https://github.com/sdepold/sequelize/pull/110) for <a href="http://sequelizejs.com/" class="link-external">Sequelize</a> which enables one to use primary key fields as foreign keys too, something I find myself doing often with my relational schemas as I don&#8217;t like using the standard <code>id</code> primary key field unless it&#8217;s the most sensible primary key to have. In practice the changes mean you can do the following (not currently possible in vanilla Sequelize):
<span id="more-1332"></span></p>

<div><pre class="brush: jscript; title: ; notranslate">
var User = sequelize.define('user', {
  name: Sequelize.STRING
},{
  underscored : true
})

var Profile = sequelize.define('profile', {
  user_id: {
    type: Sequelize.INTEGER,
    primaryKey: true
  },
  dob: Sequelize.DATE
},{
  underscored : true
})

var Profile2 = sequelize.define('profile2', {
  dob: Sequelize.DATE
},{
  underscored : true
})

User.hasOne(Profile)
User.hasOne(Profile2)

/* 
At this point Profile.user_id will act as both primary and foreign key, whereas Profile2.user_id and Profile.id will have been auto-added by Sequelize and will act as foreign key and primary key respectively.
*/
</pre></div>

<h2>Date.js alternatives</h2>

<p><a href="http://www.datejs.com/" class="link-external">Date.js</a> has been causing me a number of problems due to its nature of extending/modifying the built-in <code>Date</code> object. It really messed up my use of cookie sessions in Express, specifically to do with setting the cookie expiry dates. I still haven&#8217;t figured out exactly why cookies weren&#8217;t getting set properly but I suspect that it&#8217;s some internal change to <code>Date</code> made by date.js which was causing the problem. I then also had problems with it on the browser side of things.</p>

<p>Looking for an alternative I&#8217;m happy to have found <a href="http://arshaw.com/xdate/" class="link-external">XDate</a>, a more light-weight and better-designed date library than date.js. It wraps the default <code>Date</code> object whilst still exposing an identical API as well as providing its own. The nice thing about XDate is that its custom API methods are almost identical to those provided by date.js so switching one out for the other didn&#8217;t require any massive code changes.</p>

<p>XDate was very recently created and it&#8217;s on Github too so I expect it will only get better with time. There isn&#8217;t a node module for it yet, as far as I&#8217;m aware. Then again, I&#8217;ve just had a look and found <a href="http://momentjs.com/" class="link-external">Moment</a>, another nice-looking date library available through NPM. I might give that a whirl for my server side.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hiddentao.com/archives/2011/11/18/primary-key-foreign-key-improvements-to-sequelize-date-js-alternatives/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding model validation to the Sequelize ORM library for node.js</title>
		<link>http://www.hiddentao.com/archives/2011/11/12/adding-model-validation-to-the-sequelize-orm-library-for-node-js/</link>
		<comments>http://www.hiddentao.com/archives/2011/11/12/adding-model-validation-to-the-sequelize-orm-library-for-node-js/#comments</comments>
		<pubDate>Sat, 12 Nov 2011 15:57:08 +0000</pubDate>
		<dc:creator>ram</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[node.js]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[Sequelize]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://www.hiddentao.com/?p=1327</guid>
		<description><![CDATA[I&#8217;ve been using the Sequelize ORM library for node.js lately and it&#8217;s a really nicely done piece of work by Sascha Depold. It&#8217;s still got some way to go before one can consider it to be mature framework. One thing I miss having is a nice easy way of specifying how to validate field values [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using the <a href="http://sequelizejs.com/" class="link-external">Sequelize ORM</a> library for node.js lately and it&#8217;s a really nicely done piece of work by Sascha Depold. It&#8217;s still got some way to go before one can consider it to be mature framework. One thing I miss having is a nice easy way of specifying how to validate field values (e.g. <a href="http://docs.kohanaphp.com/libraries/validation" class="link-external">Kohana&#8217;s validation</a>). Luckily there is already the excellent <a href="https://github.com/chriso/node-validator" class="link-external">node-validator</a> library and yesterday I was able to integrate it into Sequelize to provide declarative validation for fields.
<span id="more-1327"></span></p>

<p><del datetime="2011-11-14T08:09:19+00:00">I&#8217;ve raised a <a href="https://github.com/sdepold/sequelize/pull/108" class="link-external">pull request</a> for my work</del> <strong>These changes are now in Sequelize trunk</strong>. But I&#8217;ll explain the changes here. To validate your models first define the validation for each field. For example:</p>

<div><pre class="brush: jscript; title: ; notranslate">
var User = sequelize.define 'User', {
    id: {
            type: Sequelize.INTEGER,
            autoIncrement: true,
            primaryKey: true,
    },
        name: {
            type: Sequelize.STRING,
            allowNull: false,
            unique: true,
            validate: {
                len: {
                    args: 3,
                    msg: &quot;Name must be atleast 3 characters in length&quot;
            }
        }
    },
        email: {
            type: Sequelize.STRING,
            allowNull: false,
            unique: true,
            validate: {
                len: {
                    args: [6, 128],
                    msg: &quot;Email address must be between 6 and 128 characters in length&quot;
            },
                isEmail: {
                    msg: &quot;Email address must be valid&quot;
            }
        }
    },
        password: {
            type: Sequelize.STRING,
            allowNull: false,
            validate: {
                len: {
                    args: 3
            }
        }
    }
}
</pre></div>

<p>The <code>isEmail</code> and <code>len</code> methods are just two of the many methods available (for the full list see the <a href="https://github.com/chriso/node-validator" class="link-external">node-validator page</a>). The <em>msg</em> key specifies the error message to return for a given validation if it fails. If this isn&#8217;t provided then a default generic error message will be returned by the validation library for that particular validation failure.</p>

<p>To actually perform the validation, call the validate() method on your model instance once your model has been built. For example:</p>

<div><pre class="brush: jscript; title: ; notranslate">
# build user
var user = User.build({
    name : &quot;test&quot;
    email : &quot;test&quot;
    password : &quot;test&quot;
});

# validate
errors = user.validate();
if (errors)
    # errors is an object with the entries structured as follows:
    # { field name : [list of validation failure messages] }
    #
    for (var prop in errors) {
        if (errors.hasOwnProperty(prop))
        console.log(&quot;Errors for field &quot; + prop + &quot;: &quot;);
        for (var i=0; i&lt;errors[prop].length; ++i) {
            console.log(&quot;\t&quot; + errors[prop][i]);
        }
    }
else
    # errors is null, which means validation succeeded
</pre></div>

<p>As a further improvement it would probably be good to enable custom validation methods, though this would be something we add to Sequelize rather than node-validator. Custom methods might be declared and used as follows:</p>

<div><pre class="brush: jscript; title: ; notranslate">
name: {
    type: Sequelize.STRING,
    allowNull: false,
    unique: true,
    validate: {
        len: {
            args: 3,
            msg: &quot;Name must be atleast 3 characters in length&quot;
        },
        fn: function(val) {
            if (val !== &quot;mustbethis&quot;) throw new Error(&quot;Custom validation failed&quot;);
        }
}
</pre></div>

<p>If Sascha accepts the existing validation stuff then I&#8217;ll push to include custom methods too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hiddentao.com/archives/2011/11/12/adding-model-validation-to-the-sequelize-orm-library-for-node-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CoffeeScript function binding gotcha when using cloned Spine models</title>
		<link>http://www.hiddentao.com/archives/2011/11/09/coffeescript-function-binding-gotcha-when-using-cloned-spine-models/</link>
		<comments>http://www.hiddentao.com/archives/2011/11/09/coffeescript-function-binding-gotcha-when-using-cloned-spine-models/#comments</comments>
		<pubDate>Wed, 09 Nov 2011 11:55:47 +0000</pubDate>
		<dc:creator>ram</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[CoffeeScript]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Spine]]></category>

		<guid isPermaLink="false">http://www.hiddentao.com/?p=1318</guid>
		<description><![CDATA[I&#8217;ve come across a CoffeeScript &#8220;gotcha&#8221; whilst cloning Spine models. Let&#8217;s say we have model as such: The output() method is always executed within the context of the instance of MyModel on which it gets called. Now let&#8217;s see what happens when we clone it: We expect bar.output() to return 3 yet it returns 2. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve come across a CoffeeScript &#8220;gotcha&#8221; whilst cloning <a href="http://spinejs.com/docs/models" class="link-external">Spine models</a>. Let&#8217;s say we have model as such:</p>

<div><pre class="brush: jscript; title: ; notranslate">
Spine = require(&quot;spine&quot;)
class MyModel extends Spine.Model
    id : 1
    output: () =&gt;
        console.log @id
</pre></div>

<p><span id="more-1318"></span></p>

<p>The <code>output()</code> method is always executed within the context of the instance of <code>MyModel</code> on which it gets called. Now let&#8217;s see what happens when we clone it:</p>

<div><pre class="brush: jscript; title: ; notranslate">
foo = new MyModel()
foo.id = 2
foo.output()     # outputs: 2

bar = m.clone()
bar.id = 3
bar.output()     # outputs: 2
</pre></div>

<p>We expect <code>bar.output()</code> to return 3 yet it returns 2. Why is this? It&#8217;s because the <code>output()</code> method is bound to the instance (<code>this</code>) within <code>MyModel</code>&#8216;s constructor, as can be seen from the generated Javascript:</p>

<div><pre class="brush: jscript; highlight: [15]; title: ; notranslate">
(function() {
  var MyModel, Spine, cp, m;
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
    for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
    function ctor() { this.constructor = child; }
    ctor.prototype = parent.prototype;
    child.prototype = new ctor;
    child.__super__ = parent.prototype;
    return child;
  };
  Spine = require(&quot;spine&quot;);
  MyModel = (function() {
    __extends(MyModel, Spine.Model);
    function MyModel() {
      this.output = __bind(this.output, this);
      MyModel.__super__.constructor.apply(this, arguments);
    }
    MyModel.prototype.id = 1;
    MyModel.prototype.output = function() {
      return console.log(this.id);
    };
    return MyModel;
  })();
}).call(this);
</pre></div>

<p>Thus, even when calling <code>output()</code> on a cloned object its function context will still point to the original instance it was cloned from. The solution &#8211; if you&#8217;re going to need to clone your model instance &#8211; is to not use CoffeeScript&#8217;s function binding. Using the non-binding syntax we get the results we want:</p>

<div><pre class="brush: jscript; title: ; notranslate">
Spine = require(&quot;spine&quot;)
class MyModel extends Spine.Model
    id : 1
    output: () -&gt;
        console.log @id

foo = new MyModel()
foo.id = 2
foo.output()     # outputs: 2

bar = m.clone()
bar.id = 3
bar.output()     # outputs: 3
</pre></div>
]]></content:encoded>
			<wfw:commentRss>http://www.hiddentao.com/archives/2011/11/09/coffeescript-function-binding-gotcha-when-using-cloned-spine-models/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to write a custom widget for jQuery Mobile</title>
		<link>http://www.hiddentao.com/archives/2011/11/07/how-to-write-a-custom-widget-for-jquery-mobile/</link>
		<comments>http://www.hiddentao.com/archives/2011/11/07/how-to-write-a-custom-widget-for-jquery-mobile/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 10:28:06 +0000</pubDate>
		<dc:creator>ram</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://www.hiddentao.com/?p=1307</guid>
		<description><![CDATA[I needed to write a custom widget for the jQuery Mobile library but couldn&#8217;t find any step-by-step documentation on how to do it in the official docs. A search on Google didn&#8217;t result in any better luck. In the end it turned out to be quite easy to do &#8211; I was able to figure [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to write a custom widget for the <a href="http://jquerymobile.com/" class="link-external">jQuery Mobile</a> library but couldn&#8217;t find any step-by-step documentation on how to do it in the official docs. A search on Google didn&#8217;t result in any better luck. In the end it turned out to be quite easy to do &#8211; I was able to figure it out by looking at the source code of jQuery Mobile and that of the excellent <a href="http://dev.jtsage.com/jQM-DateBox/" class="link-external">DateBox</a> plugin. In this post I outline the essentials to adding your own custom widget to jQuery Mobile.
<span id="more-1307"></span></p>

<p>Ideally place all your widget code into its own file. Here is the basic code structure:</p>

<div><pre class="brush: jscript; title: ; notranslate">
(function($){
  $.widget(&quot;mobile.mywidget&quot;, $.mobile.widget, {
    /** Available options for the widget are specified here, along with default values. */
    options: {
      inline: false,
      mode: &quot;default&quot;,
      height: 200
    },
    /** Mandatory method - automatically called by jQuery Mobile to initialise the widget. */
    _create: function() {
      var inputElement = this.element;
      var opts = $.extend(this.options, inputElement.data(&quot;options&quot;));
      $(document).trigger(&quot;mywidgetcreate&quot;);
      ...
      inputElement.after(&quot;&lt;button&gt;&quot; + inputElement.val() + &quot;&lt;/button&gt;&quot;);
      ...
    },
    /** Custom method to handle updates. */
    _update: function() {
      var inputElement = this.element;
      var opts = $.extend(this.options, inputElement.data(&quot;options&quot;));
      $(document).trigger(&quot;mywidgetupdate&quot;);
      ...
      inputElement.siblings(&quot;button&quot;).text(inputElement.val());
      ...
    },
    /* Externally callable method to force a refresh of the widget. */
    refresh: function() {
      return this._update();
    }
  });
  /* Handler which initialises all widget instances during page creation. */
  $(document).bind(&quot;pagecreate&quot;, function(e) {
    $(document).trigger(&quot;mywidgetbeforecreate&quot;);
    return $(&quot;:jqmData(role='mywidget')&quot;, e.target).mywidget();
  });
})(jQuery);
</pre></div>

<p>In the HTML we can trigger use of the widget as follows:</p>

<div><pre class="brush: xml; title: ; notranslate">
  &lt;input type=&quot;text&quot; val=&quot;test&quot; data-role=&quot;mywidget&quot; data-height=&quot;100&quot; data-inline=&quot;true&quot; /&gt;
</pre></div>

<p>In the example above our widget is known as a <em>mywidget</em>. We first define the widget and then follow it with a handler for the <a href="http://jquerymobile.com/test/docs/api/events.html" class="link-external"><code>pagecreate</code> event</a>. This handler ensures that all HTML elements with the attribute <strong>data-role=&#8221;mywidget&#8221;</strong> are processed. <em>Note that you should use the <code>jqmData</code> filter in case you&#8217;re not using the standard <code>data-</code> prefix for widget attributes</em>.</p>

<p>Any number of options can be provided to your widget with no restrictions on option type. Per-widget-instance options can be specified as HTML attributes or as dictionary keys if initializing a widget directly in Javascript. Default values for options are provided within the widget itself (as you can see above).</p>

<p>The <code>_create()</code> method gets automatically called by jQuery Mobile when we call <code>mywidget()</code> from the <code>pagecreate</code> handler. This method is responsible for setting up the initial widget display. In the example above we simply add a button after the input element with its label set to the value within the text field.</p>

<p>There is also a <code>refresh()</code> method available for the widget which can be invoked through Javascript as follows:</p>

<p><code lang="js">
$(":jqmData(role='mywidget')").mywidget("refresh");
</code></p>

<p>This in turn calls the <code>_update()</code> method which in turn refreshes the button label with the current value of the text input field. You can add as many such methods as you like to the widget. For example, most of the standard jQuery Mobile widgets also come with <code>enable()</code> and <code>disable()</code> methods.</p>

<p>You&#8217;ll also notice that we trigger widget-specific events within the functions, e.g. <code>mywidgetcreate</code>. These aren&#8217;t strictly necessary but are useful to include, especially if you want the rest of your application to know when these events take place within the widget.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hiddentao.com/archives/2011/11/07/how-to-write-a-custom-widget-for-jquery-mobile/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Comparison of RequireJS and Hem for JS minification</title>
		<link>http://www.hiddentao.com/archives/2011/10/24/comparison-of-requirejs-and-hem-for-js-minification/</link>
		<comments>http://www.hiddentao.com/archives/2011/10/24/comparison-of-requirejs-and-hem-for-js-minification/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 11:46:18 +0000</pubDate>
		<dc:creator>ram</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Hem]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[RequireJS]]></category>
		<category><![CDATA[Spine]]></category>

		<guid isPermaLink="false">http://www.hiddentao.com/?p=1301</guid>
		<description><![CDATA[As I&#8217;ve hinted at in previous posts I&#8217;ve been using RequireJS for a while now to help &#8220;modularize&#8221; my client-side Javascript code and make it easy to package it into minified and compressed script files at deployment time. And ditto for my CSS files. Recently I started using the Spine Javascript framework to base my [...]]]></description>
			<content:encoded><![CDATA[<p>As I&#8217;ve hinted at in <a href="http://www.hiddentao.com/archives/2011/08/17/notes-on-using-requirejs-with-backbone-andor-spine/" title="Notes on using RequireJS with Backbone and/or Spine" class="liinternal">previous posts</a> I&#8217;ve been using <a href="http://requirejs.org/" class="link-external">RequireJS</a> for a while now to help &#8220;modularize&#8221; my client-side Javascript code and make it easy to package it into minified and compressed script files at deployment time. And ditto for my CSS files.</p>

<p>Recently I started using the <a href="http://spinejs.com/" class="link-external">Spine</a> Javascript framework to base my app on a simple MVC architecture. Even more recently I found out that Spine&#8217;s creator Alex McCaw had written a tool called <a href="https://github.com/maccman/hem" class="link-external">Hem</a> which pretty much does whatever RequireJS except that it&#8217;s optimised for use with Spine. I gave it a go and having now used both it and RequireJS I am better placed to comment on the differences and relative advantaged of the two tools.
<span id="more-1301"></span></p>

<p>RequireJS is a standalone Javascript library (~5 KB) with no other dependencies.It introduces new syntax for packaging your Javascript code as different modules. It&#8217;s module format is based on the <a href="http://www.commonjs.org/" class="link-external">CommonJS</a> spec though its implementation reserved words and their usage differs slightly from the standard spec. RequireJS&#8217;s optimization step is yet another script which can be executed from the command-line once you&#8217;ve installed <a href="http://nodejs.org/" class="link-external">node</a>. This step will concatenate and minify your Javascript and (optionally) CSS files.</p>

<p>Hem is a command-line tool. It is an npm module and has a few npm-loaded dependencies, notably <a href="https://github.com/mishoo/UglifyJS" class="link-external">UglifyJS</a> (the same as is used by RequireJS). Thus, node and <a href="http://npmjs.org/" class="link-external">npm</a> are pre-requisites for using Hem at all. Hem integrates well with Spine in that it will package up your <em>views</em> (<code>.eco</code> or <code>.jeco</code> templates) along with your Javascript code into a single, minified script file. It also minifies and packages a CSS file you point it to into a single CSS files alongside the single script file which gets output.</p>

<p>Like RequireJS, Hem also allows code to be written in <a href="http://jashkenas.github.com/coffee-script/" class="link-external">CoffeeScript</a>, automatically running the CoffeeScript compiler before packaging things up. It&#8217;s module format closes adheres to the CommonJS spec.</p>

<p>Hem comes with two distinct options:</p>

<ul>
<li><code>server</code> &#8211; launch a simple node.js server (port 9294 by default) which concatenates all your Javascript and template files into a single Javascript file on <em>every page request</em>.</li>
<li><code>build</code> &#8211; Concatenate all your Javascript and template files into a single, minified Javascript file, ready for deployment to production.</li>
</ul>

<p>Thus, with Hem your HTML file can expect there to be available a single script file containing all of your app&#8217;s script code. This is great because it means that switching from development to production mode for your Javascript does not require any changes to the code in your HTML which references the script file. This makes it easy to test both cases, unlike  for RequireJS which may require your HTML file to work slightly differently when switching to production mode.</p>

<p>RequireJS is able to produce multiple minified output files as well as being able to concatenate multiple script files into one file based on its static analysis of <code>define()</code> and <code>require()</code> calls made within the script files. Hem supposedly analyses dependencies dynamically but in truth I think it just assumes that all script files will be needed and thus concatenates them all (including view templates) into one file to avoid errors due to missing code. The downside of putting everything into one file is that the initial download size for your web app might end up being larger (and thus more time consuming) than it would be if you were to load defer loading of some of your scripts.</p>

<p>For me it was initially weird to have to install node and npm even just for a project that only includes client-side scripting. However this comes with the benefit that it&#8217;s easy to update to the latest versions of Hem and any of the other node-based packages you may be using thanks to npm&#8217;s versioning system. As far as I&#8217;m aware the npm registry is currently the only Javascript package repository widely used by developers. What&#8217;s more, if you&#8217;re developing your server-side using node then using npm is an easy way of sharing the same packages among client and server.</p>

<p>After having used npm for a bit now I&#8217;m convinced that all future Javascript development (and specifically  the inclusion of third-party libraries into your project) will be done using npm (or a future packaging manager) as a way of managing external dependencies. For instance jQuery is already available as an npm module.  The one big downside of using npm is that its Windows support isn&#8217;t quite as good as the other platforms. So if you want to use it with windows you may have some trouble getting it running and working properly.</p>

<p>I&#8217;ve decided that I&#8217;m going to be using Hem for all my minification needs from now on since it provides everything RequireJS provides and then some (e.g. template minification). Plus it&#8217;s source code is written in CoffeeScript (unlike RequireJS), which makes it a pleasure to contribute to.</p>

<p>I&#8217;ve contributed some improvements to Hem, visible on <a href="https://github.com/maccman/hem/pulls" class="link-external">Hem&#8217;s pull requests</a> page. You can now ask it to skip CSS minification and have more control over where it should output things.</p>

<p><strong>Update (Nov 9):</strong> found an <a href="http://blog.millermedeiros.com/2011/09/amd-is-better-for-the-web-than-commonjs-modules/" class="link-external">interesting post</a> comparing the CommonJS way of defining modules to the AMD (RequireJS) way. I agree with the general sentiment expressed, in that RequireJS makes it easy to test your scripts when developing your app since it can load in dependencies on the fly from the server. Yet Hem works around this by rebuilding the entire optimised script file on every request, and thus doesn&#8217;t hinder development in any way. Moreover you get to test the final output script file that you&#8217;d get in a production build during development, which is something you can&#8217;t really do as nicely with RequireJS.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hiddentao.com/archives/2011/10/24/comparison-of-requirejs-and-hem-for-js-minification/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Notes on using RequireJS with Backbone and/or Spine</title>
		<link>http://www.hiddentao.com/archives/2011/08/17/notes-on-using-requirejs-with-backbone-andor-spine/</link>
		<comments>http://www.hiddentao.com/archives/2011/08/17/notes-on-using-requirejs-with-backbone-andor-spine/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 21:04:58 +0000</pubDate>
		<dc:creator>ram</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Backbone]]></category>
		<category><![CDATA[CoffeeScript]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[RequireJS]]></category>
		<category><![CDATA[Spine]]></category>

		<guid isPermaLink="false">http://www.hiddentao.com/?p=1296</guid>
		<description><![CDATA[For a project I&#8217;m currently working on I&#8217;ve decided to jump on the Javascript bandwagon with full weight. I intend to code in CoffeeScript, use either Spine or Backbone to give me a light-weight MVC architecture, and then deploy it all together in a clean, optimized way using RequireJS. If you&#8217;re not familiar with these [...]]]></description>
			<content:encoded><![CDATA[<p>For a project I&#8217;m currently working on I&#8217;ve decided to jump on the Javascript bandwagon with full weight. I intend to code in <a href="http://jashkenas.github.com/coffee-script/" class="link-external">CoffeeScript</a>, use either <a href="http://maccman.github.com/spine/" class="link-external">Spine</a> or <a href="http://documentcloud.github.com/backbone/" class="link-external">Backbone</a> to give me a light-weight MVC architecture, and then deploy it all together in a clean, optimized way using <a href="http://requirejs.org/" class="link-external">RequireJS</a>. If you&#8217;re not familiar with these tools and libraries then I recommend you follow the links to find out more about them. Basically, client-side Javascript-driven web app development is getting very exciting <img src='http://www.hiddentao.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p>I started off by getting Spine to to work with the <a href="https://github.com/jrburke/require-cs" class="link-external">RequireJS CoffeeScript adapter</a>. In development mode everything worked fine but when I tested using the RequireJS-optimized output some of my Spine code didn&#8217;t seem to work. Here is my HTML file:
<span id="more-1296"></span></p>

<div><pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;CoffeeScript Loader Plugin Demo&lt;/title&gt;
    &lt;script data-main=&quot;lib/main&quot; src=&quot;lib/require-jquery.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;
  &lt;a class=&quot;test&quot; href=&quot;#&quot;&gt;Test&lt;/a&gt;
&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre></div>

<p>The <code>main.js</code> script:</p>

<div><pre class="brush: jscript; title: ; notranslate">
require({
    paths: {
        cs: '../../demo/lib/cs'
    }
}, ['spine','cs!csmain']);
</pre></div>

<p>And the <code>csmain.coffee</code> script:</p>

<div><pre class="brush: jscript; title: ; notranslate">
define () -&gt;
    Tasks = Spine.Controller.create
        tag: &quot;body&quot;

        events:
            &quot;click .test&quot; : &quot;clicked&quot;

        init: -&gt;
            alert &quot;loaded&quot;

        clicked: (event) -&gt;
            alert &quot;clicked&quot;

    Tasks.init
        el: document.body
</pre></div>

<p><em>I built on top of the <a href="https://github.com/jrburke/require-cs/tree/master/demo" class="link-external">RequreJS CoffeeScript demo files</a> to save time</em>.</p>

<p>On page load I should see an alert containing the text &#8220;loaded&#8221;. When I click the link I should get an alert containing the text &#8220;clicked&#8221;. In the RequireJS-optimized version (where all the scripts were compressed and inlined into <code>main.js</code>) I got the initial page load alert but no alert when I clicked on the link. Thinking that the use of coffeescript might have jinxed it I decided to code the Spine stuff in plain old Javascript instead, to no avail. I then replaced Spine with Backbone and coded the equivalent as such:</p>

<div><pre class="brush: jscript; title: ; notranslate">
    View = Backbone.View.extend
        el: $(&quot;body&quot;)

        events:
            &quot;click .test&quot; : &quot;clicked&quot;

        initialize: -&gt;
            alert &quot;loaded&quot;

        clicked: -&gt;
            alert &quot;clicked&quot;

    new View()
</pre></div>

<p><em>CoffeeScript is beautiful, no!?</em></p>

<p>This failed in the same exact way that Spine did. So it wasn&#8217;t an issue with either of these libraries. Eventually, I took a chance and decided to not to use the combined RequireJS + jQuery script and instead opted to include them separately. This is not the most recommended way of using RequireJS with jQuery, as is clearly stated in the <a href="https://github.com/jrburke/require-jquery" class="link-external">RequireJS docs</a>. Yet this combination worked for me &#8211; when I ran the optimized version I got the click-triggered alert box showing too. I can only guess that the RequireJS + jQuery combo file breaks either jQuery or the assumptions Spine and Backbone make about jQuery.</p>

<p>So here is how I now initialize the scripts in the HTML:</p>

<div><pre class="brush: xml; title: ; notranslate">
    &lt;script src=&quot;lib/require.js&quot;&gt;&lt;/script&gt;
    &lt;script&gt;
      require({
        baseUrl: 'lib',
        priority: ['jquery']
      }, ['main']);
    &lt;/script&gt;
</pre></div>

<p>The RequireJS docs recommend additional configuration steps for the optimizer to work properly but I didn&#8217;t need to do that. My existing configuration worked fine:</p>

<div><pre class="brush: jscript; title: ; notranslate">
({
    appDir: '.',
    baseUrl: 'lib',
    //Uncomment to turn off uglify minification.
    //optimize: 'none',
    dir: '../demo-build',
    paths: {
        cs: '../../demo/lib/cs'
    },
    // Exclude CoffeeScript compiler code
    pragmasOnSave: {
        excludeCoffeeScript: true
    },
    modules: [
        {
            name: &quot;main&quot;
        }
    ]
})

</pre></div>
]]></content:encoded>
			<wfw:commentRss>http://www.hiddentao.com/archives/2011/08/17/notes-on-using-requirejs-with-backbone-andor-spine/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

