Last active
December 10, 2015 07:38
-
-
Save cburgdorf/4402616 to your computer and use it in GitHub Desktop.
A simple plugin that brings async sugar to any component that uses a card layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This plugin brings missing async features to card layouts. It can be applied | |
* on any component that uses a card layout (e.g. a NavigationView) | |
* | |
* With this plugin you can do the following: | |
* | |
* navigationView.setActiveItem(fooCard, function(){ alert('after transition'); }); | |
* navigationView.push(fooCard, function(){ alert('after transition'); }); | |
* navigationView.pop(function(){ alert('after transition'); }); | |
* | |
* You can also listen for the event 'activeitemchangecompleted' now. | |
*/ | |
Ext.define('CouchCommerce.plugins.CardLayoutEvents', { | |
extend: 'Ext.Component', | |
alias : 'plugins.cardlayoutevents', | |
init: function(parent) { | |
var me = this; | |
[ | |
'setActiveItem', | |
'push', | |
'pop' | |
].forEach(function(item){ | |
me._augmentWithCallback(parent, item); | |
}); | |
}, | |
_augmentWithCallback: function(parent, functionName){ | |
var original = parent[functionName]; | |
if (!original){ | |
//we also asyncify methods that are only available on the | |
//NavigationView (push, pop). This plugin is meant to be used | |
//on other container derived components that might lack those, | |
//hence the safety check here. | |
return; | |
} | |
parent[functionName] = function(){ | |
var lastParameter = arguments[arguments.length - 1]; | |
var isCallback = Ext.isFunction(lastParameter); | |
if (isCallback){ | |
parent.on('activeitemchange', lastParameter, parent, { | |
single: true | |
}); | |
} | |
//be aware that we call the original method with our augmented | |
//callback parameter. So we assume the parameter is not used | |
//anyway and there won't have side effects. | |
original.apply(parent, arguments); | |
} | |
} | |
}); |
Btw, if sencha had first class promises/deferred support those three methods should actually return a deferred object instead of accepting a callback. I hope someone at sencha hears me ;-)
Oh, I'm missing checks for the push and pull part because those only exist on the navigation view but the plugin can be applied to any component that uses a card layout.
I added the safety checks for push and pop
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It currently has bugs though. The handler fires two times when it shouldn't. I'll need to investigate that further.