From d3d0c066d760980235567e0f9ef3bacf40f12ab0 Mon Sep 17 00:00:00 2001 From: Massimiliano Mirra Date: Sun, 9 Oct 2011 20:29:51 +0200 Subject: [PATCH 1/4] Use PURE's render() instead of autoRender() if directives are given. autoRender() with directives leads to unexpected results. Before the fix, this test case results in the error "The node .summary-text" was not found in the template" being printed to the console and the template not being processed as expected.
[]
--- lib/plugins/sammy.pure.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/plugins/sammy.pure.js b/lib/plugins/sammy.pure.js index 09d60553..def30e76 100644 --- a/lib/plugins/sammy.pure.js +++ b/lib/plugins/sammy.pure.js @@ -11,7 +11,9 @@ Sammy.Pure = function(app, method_alias) { var pure = function(template, data, directives) { - return $(template).autoRender(data, directives); + return (typeof(directives) === 'object') ? + $(template).render(data, directives) : + $(template).autoRender(data); }; // set the default method name/extension From 3f6a86fa6d74084169d249ae55ebbef3aae9364b Mon Sep 17 00:00:00 2001 From: Massimiliano Mirra Date: Sun, 9 Oct 2011 20:33:40 +0200 Subject: [PATCH 2/4] Don't assume happy path, give some notice if load() errors out (e.g. because of JSON parse error). --- lib/sammy.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/sammy.js b/lib/sammy.js index b6070d36..8de34af6 100644 --- a/lib/sammy.js +++ b/lib/sammy.js @@ -1495,6 +1495,7 @@ data: {}, dataType: is_json ? 'json' : null, type: 'get', + error: function() { console.warn(arguments); }, success: function(data) { if (should_cache) { context.event_context.app.templateCache(location, data); From fb40b1f771e1bc9db251271bac5e4707fe2b583c Mon Sep 17 00:00:00 2001 From: Massimiliano Mirra Date: Wed, 8 Feb 2012 23:33:21 +0100 Subject: [PATCH 3/4] Allow setting an 'exit' callback for when a route is left. Example: this.get('#/foo/bar', function(context) { // business as usual }, function() { // run when user navigates away from #/foo/bar // useful e.g. to undo anything specific to the #/foo/bar view }); --- lib/sammy.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/sammy.js b/lib/sammy.js index 7f528621..36b6b99c 100644 --- a/lib/sammy.js +++ b/lib/sammy.js @@ -534,7 +534,7 @@ // It is also possible to pass a string as the callback, which is looked up as the name // of a method on the application. // - route: function(verb, path, callback) { + route: function(verb, path, callback, exit) { var app = this, param_names = [], add_route, path_match; // if the method signature is just (path, callback) @@ -543,6 +543,7 @@ path = verb; callback = path; verb = 'any'; + exit = exit; } verb = verb.toLowerCase(); // ensure verb is lower case @@ -568,7 +569,7 @@ } add_route = function(with_verb) { - var r = {verb: with_verb, path: path, callback: callback, param_names: param_names}; + var r = {verb: with_verb, path: path, callback: callback, param_names: param_names, exit: exit}; // add route to routes array app.routes[with_verb] = app.routes[with_verb] || []; // place routes in order of definition @@ -1047,6 +1048,11 @@ if (returned === false) { return false; } } } + + if(app.last_route && app.last_route.exit) { + app.last_route.exit.apply(context); + } + app.last_route = route; context.trigger('event-context-before', {context: context}); returned = route.callback.apply(context, callback_args); From 1632f9101444758c5807c0bbf7fadbe72adf549d Mon Sep 17 00:00:00 2001 From: Massimiliano Mirra Date: Wed, 8 Feb 2012 23:55:02 +0100 Subject: [PATCH 4/4] Missing bit from fb40b1f7. --- lib/sammy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sammy.js b/lib/sammy.js index 36b6b99c..e15de2df 100644 --- a/lib/sammy.js +++ b/lib/sammy.js @@ -20,7 +20,7 @@ return String(s).replace(/&(?!\w+;)/g, '&').replace(//g, '>').replace(/"/g, '"'); }, _routeWrapper = function(verb) { - return function(path, callback) { return this.route.apply(this, [verb, path, callback]); }; + return function(path, callback, exit) { return this.route.apply(this, [verb, path, callback, exit]); }; }, _template_cache = {}, _has_history = !!(window.history && history.pushState),