Permalink
Browse files

added route_set layouts

  • Loading branch information...
1 parent 0becd45 commit f0ff0ff2d8c3e69462ee2a33c99867b1f328cd60 Kellen Presley committed Jun 13, 2010
View
8 picard/lib/picard.js
@@ -5,9 +5,9 @@ require('./picard/utils')
Picard.merge({
start: function() {
- var locals = Picard.private_request_functions
+ var locals = Picard.internal_request_functions
- require('http').createServer(function(request, response) {
+ var server = require('http').createServer(function(request, response) {
Picard.merge(request, Picard.request_extensions)
request.response = response
@@ -21,7 +21,9 @@ Picard.merge({
locals._resolve.call(request)
})
- }).listen(Picard.env.port)
+ })
+
+ server.listen(Picard.env.port)
require('sys').
puts('Starting in ' + Picard.env.mode + ' mode.\n' +
View
11 picard/lib/picard/request_extensions.js
@@ -124,11 +124,16 @@ var locals = {
},
_extend_scope: function(scope){
var shared_helpers = helpers()
-
- // merge view helpers into scope
+
if ( this.route && this.route.route_set ){
+ // merge route set helpers into view scope
var merged_helpers = Picard.merge({}, shared_helpers, this.route.route_set.helpers())
scope = Picard.merge({}, merged_helpers, scope)
+
+ // use route_set layout if none defined on this scope
+ if ( typeof scope.layout == 'undefined' && this.route.route_set.layout )
+ scope.layout = this.route.route_set.layout
+
} else if ( Object.keys(shared_helpers) != 0 ) {
scope = Picard.merge({}, shared_helpers, scope)
}
@@ -281,4 +286,4 @@ var locals = {
Picard = picard = exports
Picard.request_extensions = request_extensions
-Picard.private_request_functions = locals
+Picard.internal_request_functions = locals
View
4 picard/lib/picard/routing_engine.js
@@ -9,9 +9,9 @@ var routes = {
matches = request.parsed_url().pathname.match(route.path)
if( matches ){ // incoming request matches route
- Picard.private_request_functions._extract_route_params.call(request, route, matches)
+ Picard.internal_request_functions._extract_route_params.call(request, route, matches)
try {
- request.route = route
+ request.route = route
return route.handler(request) // call programmer defined action
} catch(ex) {
request.handle_exception(ex)
View
3 sample_app/controllers/ops_controller.js
@@ -2,6 +2,7 @@ route_set('operations', function(){
var ops = this;
ops.path_prefix = '/ops'
+ ops.layout = 'application'
ops.helpers({
message: function(){
@@ -12,7 +13,7 @@ route_set('operations', function(){
// GET /ops/heartbeat
ops.get('/heartbeat', function(){
- return { text: ops.helpers().message() }
+ return { text: ops.helpers().message(), layout: 'alternate_layout' }
})
// GET /ops/version
View
11 sample_app/spec/app_spec.rb
@@ -274,6 +274,17 @@
res = Curl::Easy.perform(base_url + '/anonymous_route_two')
res.body_str.should include("123")
end
+
+ it 'should accept a layout parameter for the entire route set' do
+ res = Curl::Easy.perform(base_url + '/ops/version')
+ res.body_str.should include("http://www.w3.org/1999/xhtml") # from the doctype in the layout
+ end
+
+ it 'should allow you to override the route set template for a single route' do
+ res = Curl::Easy.perform(base_url + '/ops/heartbeat')
+ res.body_str.should include("this is another layout")
+ res.body_str.should include("for testing")
+ end
end
describe 'error handling' do
View
2 sample_app/views/advanced.haml
@@ -10,7 +10,7 @@
:each item in commands
%li= item.command
- =// messages from global "helpers" call
+ =// message comes from global call to "helpers"
#message
= message()
View
3 sample_app/views/alternate_layout.haml
@@ -0,0 +1,3 @@
+<!-- this is another layout -->
+%h1= yield()
+<!-- for testing -->
View
4 sample_app/views/ops/index.haml
@@ -2,4 +2,6 @@
%ul
%li= version
%li= message()
- %li= message('Bob')
+ <!--
+ %li= message('Bob')
+ -->

0 comments on commit f0ff0ff

Please sign in to comment.