I've created a django app which integrate jQuery.
It might seems trivial, however bear with me I can assure you there is some goodies bundled in this app.
A template tag was created to easily load any version of jQuery, locally or remotely:
{% jquery_script %}
<script type="text/javascript" src="/jq-media/jquery/1.3/jquery.js"></script>
{% jquery_script "1.2.6" %}
<script type="text/javascript" src="/jq-media/jquery/1.2.6/jquery.js"></script>
{% jquery_script "1.3" 1 %}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.js"></script>
This is my favorite part :)
I've included a small JavaScript script which allow to work with Django's named URLs on the client side. It resolve a lot of annoying issues when working with medium to large Django projects which involve lots of JavaScript.
As is there is no mechanism to achieve that. It forces us to hardcode our URLs in JavaScript, which of courses leads to unpredictable problems when URLs gets moved around. On the server side, if you used named URLs properly, you can change your URLs like you want without breaking your app, however they will not be updated on the client side.
It also becomes a problem when your root URL isn't necessarily "/" in production and in development.
Consider the following URLs:
urlpatterns = patterns('myProject.main.views',
url(r'^$', 'home', name='home'),
url(r'^/view/contact/(\d+)/$', 'view_contact', name='view-contact'),
url(r'^/calendar/(?P<year>\d{4})/$', 'view_calendar', name='view-calendar'),
)
</year>
Now let's forget the parsing bug in my code highlight that puts a </year> at the end and imagine that you want to access the view-contact view with an Ajax call.
The conventional way to achieve this would looks something like this;
$('#contact-div').load('/view/contact/'+ contact.id +'/');
If the URL gets changed to let's say *'^/contact/view/(d+)/$'*. This JavaScript code will break.
If your root URL is / in development and /something/ in prod, this JavaScript will not work in production.
My script resolve this issue gracefully while being more coherent with Django:
$('#contact-div').load($.url('view-contact', contact.id));
The script also support named arguments:
$('#calendar').load($.url('view-calendar', {year: 2009}));
To make this work you only have one template tag to add to your base.html:
{% jquery_urls %}
As now there is still jQuery UI to integrate, beside that the core features I wanted are almost all there.
I'm juggling with the idea of creating custom form widgets for jQuery UI widgets, but I'm still not sure it would be a great added value nor that it would be optimal.
I'm not sure if I will our should bundle a default jQuery UI theme..
Any inputs/feedbacks are welcome.
Special thanks to gnuvince for the help with the URL dispatcher. Cheers to everybody at Web Hosting Search for their help.
this seems useless - although im glad you like it
Won't { jquery_urls } expose all urls? I'm not sure you want to do that?
how about just using the built-in url tag? http://docs.djangoproject.com/en/dev/topics/http/urls/#id2
Adrian - presumably because he's doing this in a static JS file, not a template.
But Nix's point is valid. I use a different method - I create an attribute like 'data-callback-url="{% url callback-url-name %}"' on an HTML element, then use $(this).attr("data-callback-url") to get the URL in JS code.
@Nix:
That's a valid concern and should be considered when using it in a project.
@Adrian:
The built-in URL tag works fine for parameter-less URLs, but there is some downsides;
I'm glad I've posted this code since Nix's point and Adam's trick will probably bring some positive changes in my code.
Thanks for you feedback guys
Pretty cool project, I usually doing stuff like @Adam Gomaa
Nice project, I hit the same wall when I split my code into more modules all ajax named urls broke. I would use this app if it weren't for the security concern.