Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swap callback patch #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions lib/sammy.js
Original file line number Diff line number Diff line change
Expand Up @@ -1131,25 +1131,37 @@

// Swaps the content of `$element()` with `content`
// You can override this method to provide an alternate swap behavior
// for `EventContext.partial()`.
// for `EventContext.partial()`.
//
// ### Example
//
// var app = $.sammy(function() {
//
// // implements a 'fade out'/'fade in'
// this.swap = function(content) {
// this.$element().hide('slow').html(content).show('slow');
// }
// // the fade function is async so wapping content should be done after the element has been hidden
// this.swap = function(content, callback) {
//
// var _this = this;
//
// this.$element().fadeOut('slow', function(){
// _this.$element().html(content)
// _this.$element().fadeIn('slow',
// function(){
// if(callback) callback.apply();
// });
// });
// };
//
// get('#/', function() {
// this.partial('index.html.erb') // will fade out and in
// });
//
// });
//
swap: function(content) {
return this.$element().html(content);
swap: function(content, callback) {
var r = this.$element().html(content);
if(callback) callback.apply();
return r;
},

// a simple global cache for templates. Uses the same semantics as
Expand Down Expand Up @@ -1539,9 +1551,21 @@

// `render()` the 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) {

// if the method signature is just (location, callback)
if (location && _isFunction(callback))
return this.render(location, data, function(){return callback}).swap();

// if the method signature is just (location, callback)
if (!callback && _isFunction(data))
return this.render(location, null, function(){return data}).swap();

// if the method signature is just (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.
// The function can accept any number of arguments as long as the last
Expand Down Expand Up @@ -1648,8 +1672,8 @@

// executes `EventContext#swap()` with the `content`
swap: function() {
return this.then(function(content) {
this.event_context.swap(content);
return this.then(function(callback, content) {
this.event_context.swap(content, callback);
}).trigger('changed', {});
},

Expand Down Expand Up @@ -1811,8 +1835,8 @@

// `render()` the 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 @@ -1877,8 +1901,8 @@
},

// A shortcut to app's `swap()`
swap: function(contents) {
return this.app.swap(contents);
swap: function(contents, callback, data) {
return this.app.swap(contents, callback);
},

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