Skip to content

Commit

Permalink
Remove hard-coded URLs from templates client-side code.
Browse files Browse the repository at this point in the history
Closes #13
  • Loading branch information
ericf committed Jan 22, 2013
1 parent b86ceeb commit 01e8dc3
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 9 deletions.
1 change: 1 addition & 0 deletions app.js
Expand Up @@ -107,6 +107,7 @@ app.get('/templates.js', routes.templates);
// **Note:** This needs to be the last route.
app.get('/:place', routes.places.lookup('/places/'));

PNM_ENV.ROUTES = exposedRoutes;
app.expose(exposedRoutes, 'YUI.Env.PNM.ROUTES', 'pnm_env');

// -- Exports ------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions conf/config.json
Expand Up @@ -70,6 +70,13 @@
"pnm-photo",
"yql"
]
},

"pnm-helpers": {
"path" : "helpers.js",
"requires": [
"array-extras"
]
}
},

Expand Down Expand Up @@ -121,6 +128,7 @@
"pnm-no-location-view",
"pnm-photos",
"pnm-place",
"pnm-helpers",
"pnm-templates"
]
}
Expand Down
3 changes: 2 additions & 1 deletion lib/hbs.js
@@ -1,10 +1,11 @@
var exphbs = require('express3-handlebars'),
Y = require('yui').use('handlebars'),
Y = require('yui').use('handlebars', 'pnm-helpers'),

config = require('../conf/config');

module.exports = exphbs.create({
defaultLayout: config.layouts.main,
handlebars : Y.Handlebars,
helpers : Y.PNM.Helpers,
partialsDir : config.dirs.templates
});
11 changes: 9 additions & 2 deletions public/js/app.js
Expand Up @@ -89,7 +89,7 @@ PhotosNearMe = Y.Base.create('photosNearMe', Y.App, [], {
var place = new Place(res.coords);
place.load(function () {
self.set('place', place);
self.navigate('/places/' + place.get('id') + '/', {replace: true});
self.navigateToRoute('places', place, {replace: true});
});
});
},
Expand Down Expand Up @@ -175,8 +175,14 @@ PhotosNearMe = Y.Base.create('photosNearMe', Y.App, [], {
});
},

navigateToRoute: function (routeName, context, options) {
var path = Y.PNM.Helpers.pathTo(routeName, context);
if (!path) { return false; }
return this.navigate(path, options);
},

navigateToPhoto: function (e) {
this.navigate('/photos/' + e.photo.get('id') + '/');
this.navigateToRoute('photos', e.photo);
}

}, {
Expand All @@ -201,6 +207,7 @@ Y.namespace('PNM').App = PhotosNearMe;
"pnm-no-location-view",
'pnm-photos',
'pnm-place',
'pnm-helpers',
'pnm-templates'
]
});
4 changes: 2 additions & 2 deletions public/js/views/lightbox.js
Expand Up @@ -44,12 +44,12 @@ LightboxView = Y.Base.create('lightboxView', Y.View, [], {

if (prev) {
prev.loadImg();
nav.prev = prev.get('id');
nav.prev = prev;
}

if (next) {
next.loadImg();
nav.next = next.get('id');
nav.next = next;
}
}

Expand Down
39 changes: 39 additions & 0 deletions shared/js/helpers.js
@@ -0,0 +1,39 @@
YUI.add('pnm-helpers', function (Y) {

var PNM_ENV = YUI.namespace('Env.PNM');

regexPathParam = /([:*])([\w\-]+)?/g;

Y.namespace('PNM').Helpers = {
pathTo: function (routeName, context) {
context || (context = this);

var route = PNM_ENV.ROUTES[routeName],
path, keys;

if (!route) { return ''; }

path = route.path;
keys = Y.Array.map(route.keys, function (key) { return key.name; });

if (context && keys.length) {
if (context._isYUIModel) {
context = context.getAttrs(keys);
}

Y.Array.each(keys, function (key) {
var regex = new RegExp('[:*]' + key + '\\b');
path = path.replace(regex, context[key]);
});
}

// Replace missing params with empty strings.
return path.replace(regexPathParam, '');
}
};

}, '0.6.0', {
requires: [
'array-extras'
]
});
2 changes: 1 addition & 1 deletion shared/templates/grid-photo.handlebars
@@ -1,5 +1,5 @@
<li class="photo">
<a href="/photos/{{id}}/">
<a href='{{pathTo "photos" this}}'>
<img title="{{title}}" src="{{thumbUrl}}"
alt="Thumbnail of a photo with title: {{title}}" />
</a>
Expand Down
2 changes: 1 addition & 1 deletion shared/templates/header.handlebars
Expand Up @@ -5,7 +5,7 @@
</a>

{{#if place}}
<a href="/places/{{place.id}}/">
<a href='{{pathTo "places" place}}'>
Photos Near <span id="location">{{place.text}}</span>
</a>
{{else}}
Expand Down
4 changes: 2 additions & 2 deletions shared/templates/lightbox.handlebars
@@ -1,11 +1,11 @@
{{#if nav}}
<ul class="lightbox-nav layout">
{{#if nav.prev}}
<li class="prev"><a href="/photos/{{nav.prev}}/">« Previous</a></li>
<li class="prev"><a href='{{pathTo "photos" nav.prev}}'>« Previous</a></li>
{{/if}}

{{#if nav.next}}
<li class="next"><a href="/photos/{{nav.next}}/">Next »</a></li>
<li class="next"><a href='{{pathTo "photos" nav.next}}'>Next »</a></li>
{{/if}}
</ul>
{{/if}}
Expand Down
5 changes: 5 additions & 0 deletions views/layouts/main.handlebars
Expand Up @@ -50,6 +50,11 @@
photos = new Y.PNM.Photos({items: data.photos ? Y.JSON.parse(data.photos) : []});
photo = new Y.PNM.Photo(data.photo ? Y.JSON.parse(data.photo) : {});
// Register template helpers.
Y.Object.each(Y.PNM.Helpers, function (helper, name) {
Y.Handlebars.registerHelper(name, helper);
});
// Create app.
app = new Y.PNM.App({
place : place,
Expand Down

0 comments on commit 01e8dc3

Please sign in to comment.