Skip to content

Commit

Permalink
Fixed issue #101: swap callback
Browse files Browse the repository at this point in the history
Based on enix's patch, cleaned it up and fixed a couple minor issues
  • Loading branch information
dfunckt authored and quirkey committed Jan 21, 2012
1 parent 6ad01ad commit 4a301f2
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions lib/sammy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 4a301f2

Please sign in to comment.