backbone-commands

Backbone commands

backbone-commands

The package can be installed through npm or bower.

npm install backbone-commands
bower install backbone-commands

After installing the package, you can grab the backbone-commands.js or the minified version of it backbone-commands.min.js

// Create an instance of Backbone.Commands where we can do some bindings. 
var commands = new Backbone.Commands();
 
// Bind a Backbone.Command class (note that StartupCommand is not an instance). 
commands.bind('startup', StartupCommand);
 
// Trigger the command. 
commands.trigger('startup');
// Assume we bind the following command under startup. 
var StartupCommand = Backbone.Command.extend({
    executefunction(messageuser){
        console.log('Started with message '+message+' by user '+user);
    }
});
 
// Trigger the command with some payload. 
commands.trigger('startup', 'hello world', 'R. Moorman');
// Assume we bind the following command under startup. 
var StartupCommand = Backbone.MacroCommand.extend({
    initializefunction(){
        this.addSubCommand(DefineModelCommand);
        this.addSubCommand(DefineViewCommand);
        this.addSubCommand(DefineControllerCommand);
    }
});
 
// Trigger the command. 
commands.trigger('startup');
// ASync load of configuration via a provided API endpoint. 
var LoadConfigCommand = Backbone.ASyncCommand.extend({
    executefunction(address){
        // Assume a model is available within this command 
        this.model.fetch({
            url: address,
            success: this.completeCommand
        });
    }
});
 
// Assume we bind the following command under startup. 
var StartupCommand = Backbone.ASyncMacroCommand.extend({
    initializefunction(){
        this.addSubCommand(LoadConfigCommand);
        this.addSubCommand(DefineModelCommand);
        this.addSubCommand(DefineViewCommand);
        this.addSubCommand(DefineControllerCommand);
    }
});
 
// Trigger the command. 
commands.trigger('startup', 'http://localhost/api/config/?format=json');

Note that when you are using an ASyncCommand, you should call completeCommand when your command has finished it's async logic. The context of completeCommand is already bound to the command itself, so callbacks directly to completeCommand from async loaders like jQuery.ajax or Backbone.Model are supported.

0.2.0

  • Added usage examples in readme
  • Added Backbone.ASyncCommand and ASyncMacroCommand
  • Added Backbone.MacroCommand

0.1.0

  • Prototype