Skip to content

Commit 4a301f2

Browse files
dfuncktquirkey
authored andcommitted
Fixed issue #101: swap callback
Based on enix's patch, cleaned it up and fixed a couple minor issues
1 parent 6ad01ad commit 4a301f2

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

lib/sammy.js

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,18 +1181,26 @@
11811181
// var app = $.sammy(function() {
11821182
//
11831183
// // implements a 'fade out'/'fade in'
1184-
// this.swap = function(content) {
1185-
// this.$element().hide('slow').html(content).show('slow');
1186-
// }
1187-
//
1188-
// get('#/', function() {
1189-
// this.partial('index.html.erb') // will fade out and in
1190-
// });
1184+
// this.swap = function(content, callback) {
1185+
// var context = this;
1186+
// context.$element().fadeOut('slow', function() {
1187+
// context.$element().html(content);
1188+
// context.$element().fadeIn('slow', function() {
1189+
// if (callback) {
1190+
// callback.apply();
1191+
// }
1192+
// });
1193+
// });
1194+
// };
11911195
//
11921196
// });
11931197
//
1194-
swap: function(content) {
1195-
return this.$element().html(content);
1198+
swap: function(content, callback) {
1199+
var $el = this.$element().html(content);
1200+
if (_isFunction(callback)) {
1201+
callback.apply();
1202+
}
1203+
return $el;
11961204
},
11971205

11981206
// a simple global cache for templates. Uses the same semantics as
@@ -1587,8 +1595,28 @@
15871595

15881596
// `render()` the `location` with `data` and then `swap()` the
15891597
// app's `$element` with the rendered content.
1590-
partial: function(location, data) {
1591-
return this.render(location, data).swap();
1598+
partial: function(location, data, callback) {
1599+
1600+
// invoked as partial(location, data, callback)
1601+
if (location && _isFunction(callback)) {
1602+
return this.render(location, data, function() {
1603+
return callback;
1604+
}).swap();
1605+
}
1606+
1607+
// invoked as partial(location, callback)
1608+
if (!callback && _isFunction(data)) {
1609+
return this.render(location, null, function() {
1610+
return data;
1611+
}).swap();
1612+
}
1613+
1614+
// invoked as partial(location)
1615+
if (location && !callback && !data) {
1616+
return this.render(location, null, function() {
1617+
return function() {};
1618+
}).swap();
1619+
}
15921620
},
15931621

15941622
// defers the call of function to occur in order of the render queue.
@@ -1859,8 +1887,8 @@
18591887

18601888
// `render()` the `location` with `data` and then `swap()` the
18611889
// app's `$element` with the rendered content.
1862-
partial: function(location, data) {
1863-
return new Sammy.RenderContext(this).partial(location, data);
1890+
partial: function(location, data, callback) {
1891+
return new Sammy.RenderContext(this).partial(location, data, callback);
18641892
},
18651893

18661894
// create a new `Sammy.RenderContext` calling `send()` with an arbitrary
@@ -1925,8 +1953,8 @@
19251953
},
19261954

19271955
// A shortcut to app's `swap()`
1928-
swap: function(contents) {
1929-
return this.app.swap(contents);
1956+
swap: function(contents, callback) {
1957+
return this.app.swap(contents, callback);
19301958
},
19311959

19321960
// Raises a possible `notFound()` error for the current path.

0 commit comments

Comments
 (0)