diff --git a/lib/sammy.js b/lib/sammy.js index 05869142..efadd35e 100644 --- a/lib/sammy.js +++ b/lib/sammy.js @@ -1181,18 +1181,26 @@ // var app = $.sammy(function() { // // // implements a 'fade out'/'fade in' - // this.swap = function(content) { - // this.$element().hide('slow').html(content).show('slow'); - // } - // - // get('#/', function() { - // this.partial('index.html.erb') // will fade out and in - // }); + // this.swap = function(content, callback) { + // var context = this; + // context.$element().fadeOut('slow', function() { + // context.$element().html(content); + // context.$element().fadeIn('slow', function() { + // if (callback) { + // callback.apply(); + // } + // }); + // }); + // }; // // }); // - swap: function(content) { - return this.$element().html(content); + swap: function(content, callback) { + var $el = this.$element().html(content); + if (_isFunction(callback)) { + callback.apply(); + } + return $el; }, // a simple global cache for templates. Uses the same semantics as @@ -1587,8 +1595,28 @@ // `render()` the `location` with `data` and then `swap()` the // app's `$element` with the rendered content. - partial: function(location, data) { - return this.render(location, data).swap(); + partial: function(location, data, callback) { + + // invoked as partial(location, data, callback) + if (location && _isFunction(callback)) { + return this.render(location, data, function() { + return callback; + }).swap(); + } + + // invoked as partial(location, callback) + if (!callback && _isFunction(data)) { + return this.render(location, null, function() { + return data; + }).swap(); + } + + // invoked as partial(location) + if (location && !callback && !data) { + return this.render(location, null, function() { + return function() {}; + }).swap(); + } }, // defers the call of function to occur in order of the render queue. @@ -1859,8 +1887,8 @@ // `render()` the `location` with `data` and then `swap()` the // app's `$element` with the rendered content. - partial: function(location, data) { - return new Sammy.RenderContext(this).partial(location, data); + partial: function(location, data, callback) { + return new Sammy.RenderContext(this).partial(location, data, callback); }, // create a new `Sammy.RenderContext` calling `send()` with an arbitrary @@ -1925,8 +1953,8 @@ }, // A shortcut to app's `swap()` - swap: function(contents) { - return this.app.swap(contents); + swap: function(contents, callback) { + return this.app.swap(contents, callback); }, // Raises a possible `notFound()` error for the current path.