Last active
July 9, 2021 20:14
-
-
Save kkemple/8bdc20cae7cdda6b7e53 to your computer and use it in GitHub Desktop.
Konami code Marionette Behavior
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
'use strict'; | |
var Marionette = require('backbone.marionette'), | |
Konami; | |
Konami = Marionette.Behavior.extend({ | |
defaults: { | |
code : [38, 38, 40, 40, 37, 39, 37, 39, 66, 65] | |
}, | |
events: { | |
'keyup': '_konami', | |
}, | |
initialize: function() { | |
this.cache = []; | |
}, | |
_konami: function(e) { | |
if (this.options.code.length > this.cache.push(e.which)) return; | |
if (this.options.code.length < this.cache.length) this.cache.shift(); | |
if (this.options.code.toString() !== this.cache.toString()) return; | |
this.view.trigger('konami'); | |
this.view.triggerMethod('konami'); | |
} | |
}); | |
module.exports = Konami; | |
/** | |
* Usage: | |
* | |
* var View = Marionette.ItemView.extend({ | |
* ... | |
* behaviors: { | |
* Konami: { | |
* behaviorClass: Konami | |
* } | |
* }, | |
* onKonami: function() { | |
* // easter egg! | |
* } | |
* }); | |
*/ |
thanks, yeah i made a vanilla JS version like a year ago, I figured why not port it over to Marionette 😄
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love this.