Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

PURE plugin fix & warn on load error #113

Open
wants to merge 5 commits into from

2 participants

@bard

Deals with a couple of issues that bit me on my first day with SammyJS.

The second is more of a beginning of a fix (I haven't looked into Sammy's own logging facilities yet).

bard added some commits
@bard bard 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.

    <!DOCTYPE html>
    <html>
      <head>
        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script type="text/javascript" src="js/pure.js"></script>
        <script type="text/javascript" src="js/sammy/sammy.js"></script>
        <script type="text/javascript" src="js/sammy/plugins/sammy.pure.js"></script>
        <script type="text/javascript">
            var app = $.sammy('#main', function() {
                this.use('Pure');

                this.get('#/', function(context) {
                    var data = {
                        summary: 'buy bread',
                        state: 'done'
                    };
                    var directives = {
                        '.summary-text': 'summary',
                        '.state': 'state'
                    };

                    context.pure('#main', data, directives);
                });
            });

            $(document).ready(function() {
                app.run('#/');
            });
        </script>
      </head>
      <body>
        <div id="main">
          <div class="summary">[<span class="state"></span>] <span class="summary-text"></span></div>
        </div>
      </body>
    </html>
d3d0c06
@bard bard Don't assume happy path, give some notice if load() errors out (e.g. …
…because of JSON parse error).
3f6a86f
@quirkey

I think this should probably actually throw an error (throw()) so the routes/EventContext can catch/deal with it.

bard added some commits
@bard bard Merge remote-tracking branch 'remotes/upstream/master' a1faf5a
@bard bard 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
});
fb40b1f
@bard bard Missing bit from fb40b1f. 1632f91
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 9, 2011
  1. @bard

    Use PURE's render() instead of autoRender() if directives are given. …

    bard authored
    …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.
    
        <!DOCTYPE html>
        <html>
          <head>
            <script type="text/javascript" src="js/jquery.min.js"></script>
            <script type="text/javascript" src="js/pure.js"></script>
            <script type="text/javascript" src="js/sammy/sammy.js"></script>
            <script type="text/javascript" src="js/sammy/plugins/sammy.pure.js"></script>
            <script type="text/javascript">
                var app = $.sammy('#main', function() {
                    this.use('Pure');
    
                    this.get('#/', function(context) {
                        var data = {
                            summary: 'buy bread',
                            state: 'done'
                        };
                        var directives = {
                            '.summary-text': 'summary',
                            '.state': 'state'
                        };
    
                        context.pure('#main', data, directives);
                    });
                });
    
                $(document).ready(function() {
                    app.run('#/');
                });
            </script>
          </head>
          <body>
            <div id="main">
              <div class="summary">[<span class="state"></span>] <span class="summary-text"></span></div>
            </div>
          </body>
        </html>
  2. @bard

    Don't assume happy path, give some notice if load() errors out (e.g. …

    bard authored
    …because of JSON parse error).
Commits on Feb 8, 2012
  1. @bard
  2. @bard

    Allow setting an 'exit' callback for when a route is left.

    bard authored
    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
    });
  3. @bard

    Missing bit from fb40b1f.

    bard authored
This page is out of date. Refresh to see the latest.
Showing with 13 additions and 4 deletions.
  1. +3 −1 lib/plugins/sammy.pure.js
  2. +10 −3 lib/sammy.js
View
4 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
View
13 lib/sammy.js
@@ -20,7 +20,7 @@
return String(s).replace(/&(?!\w+;)/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
},
_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),
@@ -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);
@@ -1524,6 +1530,7 @@
data: {},
dataType: is_json ? 'json' : 'text',
type: 'get',
+ error: function() { console.warn(arguments); },
success: function(data) {
if (should_cache) {
context.event_context.app.templateCache(location, data);
Something went wrong with that request. Please try again.