Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Context#executeCommand #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,21 @@ return function FooCommand(context, eventName, eventData){
N.B.: We'd strongly advise against using the context as a service locator inside commands.
If your command requires any additional dependencies it's best practice to turn it into a "real" command (which exposes an `execute` method and declares its dependencies through the `wiring` property.)

### Direct command execution

It's also possible to execute commands directly, without dispatching an event.

```js
context.executeCommand(ComplexUpdateMechanicCommand, {
eventName : "update:complex:mechanic",
eventData: {
importantStuff: "Holy shizzles!"
}
} );
```

This will execute the command in exactly the same vein as when triggered by an event.

### Use underscore to reduce boilerplate

You can use the underscore `extend` method to conform your command declarations to your other object declarations:
Expand Down
31 changes: 20 additions & 11 deletions backbone.geppetto.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,25 @@
});
};

Geppetto.Context.prototype._executeCommand = function(CommandConstructor, eventName, eventData, wiring) {
var commandInstance = new CommandConstructor(this, eventName, eventData);

commandInstance.context = this;
commandInstance.eventName = eventName;
commandInstance.eventData = eventData;
this.resolver.resolve(commandInstance, wiring);
if (_.isFunction(commandInstance.execute)) {
commandInstance.execute();
}
};

Geppetto.Context.prototype.executeCommand = function(CommandConstructor, event) {
if (!event) {
event = {};
}
this._executeCommand(CommandConstructor, event.eventName, event.eventData);
};

Geppetto.Context.prototype.wireCommand = function wireCommand(eventName, CommandConstructor, wiring) {

var _this = this;
Expand All @@ -369,17 +388,7 @@
}

this.vent.listenTo(this.vent, eventName, function(eventData) {

var commandInstance = new CommandConstructor(_this, eventName, eventData);

commandInstance.context = _this;
commandInstance.eventName = eventName;
commandInstance.eventData = eventData;
_this.resolver.resolve(commandInstance, wiring);
if (_.isFunction(commandInstance.execute)) {
commandInstance.execute();
}

_this._executeCommand(CommandConstructor, eventName, eventData, wiring);
}, this);
};

Expand Down
46 changes: 46 additions & 0 deletions specs/src/geppetto-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,52 @@ define([
expect(command.ctorArgs).to.contain(context);
});
});

describe('when executing commands', function(){
var context;
var CommandClass;
var command;
beforeEach(function() {
var ContextDefinition = Geppetto.Context.extend({});
context = new ContextDefinition();
CommandClass = function() {
this.ctorArgs = _.toArray(arguments);
};
CommandClass.prototype.execute = function() {
command = this;
};

});
afterEach(function() {
context.destroy();
context = null;
CommandClass = null;
command = null;
});
it("should resolve dependencies", function(){
var value = {};
context.wireValue('value', value);
CommandClass.prototype.wiring = ['value'];
context.executeCommand(CommandClass);
expect(command.value).to.equal(value);
});
it("should inject event parameter as members", function(){
var eventName = "test:event";
var eventData = {
foo : "foo"
};
context.executeCommand(CommandClass, {
eventName : eventName,
eventData : eventData
});
expect(command.eventName).to.equal(eventName);
expect(command.eventData).to.equal(eventData);
});
it("should inject the context", function(){
context.executeCommand(CommandClass);
expect(command.context).to.equal(context);
});
});

describe("when wiring dependencies in batch using the Context 'wiring' parameter", function() {

Expand Down