GithubHelp home page GithubHelp logo

mattbryson / backbone.marionette.keyshortcuts Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 3.0 8 KB

An extension to Backbone.Marionette.View that adds a "keyShortcuts" hash to the view object, allowing you to register keyboard commands.

License: MIT License

JavaScript 100.00%

backbone.marionette.keyshortcuts's Introduction

backbone.marionette.keyshortcuts

An extension to Backbone.Marionette.View that adds a keyShortcuts hash to the view object, allowing you to register keyboard commands using Mousetrap.js.

Dependencies

Installation

npm install backbone.marionette.keyshortcuts

If you are using requireJS then you need to ensure that mousetrap can be found on the default path, or is defined in your require config.

    paths: {
      'backbone.marionette.keyShortcuts': '/vendor/marionette/backbone.marionette.keyshortcuts',
      'mousetrap':'/vendor/mousetrap/mousetrap.min'
    }

Usage

In your View class, simply add a keyShortcuts like you would for the events hash.

It supports single keys and key combinations. To define the event trigger, add a colon followed by the event name after your key combination; 'd:keyup'.

View.MyForm = Marionette.ItemView.extend({
 tagName: 'form',
 keyShortcuts:{
  'command+s' : 'save',
  'up up down left' : function() { console.log('cheat!') },
  'd:keyup' : function() { console.log('d key was released') },
 },
 events: {
  'click @saveBtn' : 'save',
 },
 ui: {
  'saveBtn' : '.save'
 },
 save: function(e) {
  e.preventDefault(); //stop the browser saving..
  this.model.save();
 }
});

Or you can define them in a Behavior class

ShortcutsBehaviour = Marionette.Behavior.extend({
 keyShortcuts: {
  "backspace": "delete",
  "del": "delete"
 },
 
 delete:functio(e) {
  e.preventDefault(); //stop the browser from navigating back
  console.log("Delete something!");
 }
}
View.MyForm = Marionette.ItemView.extend({
 tagName: 'form',
 behaviors: {
  ShortcutsBehaviour: {
   behaviorClass: ShortcutsBehaviour
  }
 },
});

Info

See https://craig.is/killing/mice for how to use Mousetrap.

backbone.marionette.keyshortcuts's People

Contributors

mattbryson avatar paulfalgout avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.