|
1181 | 1181 | // var app = $.sammy(function() {
|
1182 | 1182 | //
|
1183 | 1183 | // // 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 | + // }; |
1191 | 1195 | //
|
1192 | 1196 | // });
|
1193 | 1197 | //
|
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; |
1196 | 1204 | },
|
1197 | 1205 |
|
1198 | 1206 | // a simple global cache for templates. Uses the same semantics as
|
|
1587 | 1595 |
|
1588 | 1596 | // `render()` the `location` with `data` and then `swap()` the
|
1589 | 1597 | // 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 | + } |
1592 | 1620 | },
|
1593 | 1621 |
|
1594 | 1622 | // defers the call of function to occur in order of the render queue.
|
|
1859 | 1887 |
|
1860 | 1888 | // `render()` the `location` with `data` and then `swap()` the
|
1861 | 1889 | // 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); |
1864 | 1892 | },
|
1865 | 1893 |
|
1866 | 1894 | // create a new `Sammy.RenderContext` calling `send()` with an arbitrary
|
|
1925 | 1953 | },
|
1926 | 1954 |
|
1927 | 1955 | // 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); |
1930 | 1958 | },
|
1931 | 1959 |
|
1932 | 1960 | // Raises a possible `notFound()` error for the current path.
|
|
0 commit comments