Skip to content

Instantly share code, notes, and snippets.

@mildfuzz
Last active October 12, 2015 14:07
Show Gist options
  • Select an option

  • Save mildfuzz/4037914 to your computer and use it in GitHub Desktop.

Select an option

Save mildfuzz/4037914 to your computer and use it in GitHub Desktop.
Behaviour Manager
/* Store for localised behaviours
*
* Requires jquery and Mild Fuzz's plugins.js (http://goo.gl/MVcH2)
* @mildfuzz
*
* Usage
*
* Create behaviour within case:
* case "BehaviourString":
* //some function
* break;
*
* Assign a behaviour to an element:
* <div class='behaves' data-behave='required-action'>
* Supports space separated list of behaviours.
*/
(function($){$(document).ready(function(){
$('.behaves').each(function(){
if($(this).hasAttr('data-behave')){
var behaviours = $(this).attr('data-behave').split(' ');
var object = this;
$(behaviours).each(function(){
$(object).mfBehaviours(String(this)).addClass(String(this));
});
}
});
});})(jQuery);
(function(){
$.fn.mfBehaviours = function(behaviour){
switch (behaviour){
case "BehaviourString":
//easy delegation of events
$(document).on('event','.BehaviourString',function(){
//do stuff
});
break;
case "otherBehaviourString":
//some other action
break;
default:
if(typeof $.fn.highlight === "function" && typeof String.prototype.simpleTemplate === "function"){
$(this).highlight();
console.error('This behaviour !-1-! does not exist. Associated objects have been highlighted'.simpleTemplate(behaviour));
} else {
console.error('This behaviour does not exist.');
}
break;
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment