forked from kbparagua/paloma
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kbparagua#91 from kbparagua/feature/before-callbacks
Feature/before callbacks
- Loading branch information
Showing
6 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
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
79 changes: 79 additions & 0 deletions
79
test_app/spec/javascripts/before_callback_performer_spec.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
describe('Paloma.BeforeCallbackPerformer', function(){ | ||
describe('#perform(action)', function(){ | ||
|
||
var Controller = Paloma.controller('MyController', { | ||
before: [ | ||
'all -> initialize doThat', | ||
'singleCallback unknown -> doSomething', | ||
'multiCallback -> doSomething doAnotherThing' | ||
], | ||
|
||
initialize: function(){ | ||
this.didWhat = []; | ||
}, | ||
|
||
did: function(what){ return this.didWhat.indexOf(what) >= 0; }, | ||
|
||
noCallback: function(){ console.log('I have no callback.'); }, | ||
singleCallback: function(){ console.log('Single callback.'); }, | ||
multiCallback: function(){ console.log('Multiple callbacks.')}, | ||
|
||
doThat: function(){ this.didWhat.push('that'); }, | ||
doSomething: function(){ this.didWhat.push('something'); }, | ||
doAnotherThing: function(){ this.didWhat.push('anotherThing') } | ||
}); | ||
|
||
function itPerformsCallbackForAll(controller){ | ||
it('performs callback for all', function(){ | ||
expect( controller.did('that') ).toBeTruthy(); | ||
}); | ||
}; | ||
|
||
describe('when there is no matched callback', function(){ | ||
var controller = new Controller(), | ||
performer = new Paloma.BeforeCallbackPerformer(controller); | ||
|
||
performer.perform('noCallback'); | ||
|
||
it('will not perform any callback', function(){ | ||
expect( controller.did('something') ).toBeFalsy(); | ||
}); | ||
|
||
itPerformsCallbackForAll(controller); | ||
}); | ||
|
||
describe('when there is one matched callback', function(){ | ||
var controller = new Controller(), | ||
performer = new Paloma.BeforeCallbackPerformer(controller); | ||
|
||
performer.perform('singleCallback'); | ||
|
||
it('will perform the matched callback', function(){ | ||
expect( controller.did('something') ).toBeTruthy(); | ||
}); | ||
|
||
itPerformsCallbackForAll(controller); | ||
}); | ||
|
||
describe('when there is more than one matched callbacks', function(){ | ||
var controller = new Controller(), | ||
performer = new Paloma.BeforeCallbackPerformer(controller); | ||
|
||
performer.perform('multiCallback'); | ||
|
||
it('will perform all the matched callbacks', function(){ | ||
expect( controller.did('something') && controller.did('anotherThing') ). | ||
toBeTruthy(); | ||
}); | ||
|
||
it('will perform the callbacks in order of definition', function(){ | ||
expect(controller.didWhat).toEqual([ | ||
'that', 'something', 'anotherThing' | ||
]); | ||
}); | ||
|
||
itPerformsCallbackForAll(controller); | ||
}); | ||
|
||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
Paloma.BaseController = function(params){ | ||
this.params = params; | ||
}; | ||
|
||
Paloma.BaseController.prototype = { | ||
before: [] | ||
}; | ||
|
69 changes: 69 additions & 0 deletions
69
vendor/assets/javascripts/paloma/before_callback_performer.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
Paloma.BeforeCallbackPerformer = function(controller){ | ||
this.controller = controller; | ||
this.entries = controller.before; | ||
this.action = null; | ||
}; | ||
|
||
Paloma.BeforeCallbackPerformer.prototype = { | ||
|
||
perform: function(action){ | ||
this.action = action; | ||
this._executeCallbacks(); | ||
}, | ||
|
||
_executeCallbacks: function(){ | ||
for (var i = 0, n = this._callbacks().length; i < n; i++) | ||
this._executeCallback( this._callbacks()[i] ); | ||
}, | ||
|
||
_executeCallback: function(name){ | ||
var callback = this.controller[name]; | ||
if (callback) callback.call(this.controller); | ||
}, | ||
|
||
_callbacks: function(){ | ||
if (this._callbackList) return this._callbackList; | ||
|
||
this._callbackList = []; | ||
|
||
for (var i = 0, n = this.entries.length; i < n; i++){ | ||
var entry = this.entries[i]; | ||
|
||
this._callbackList = | ||
this._callbackList.concat( this._getCallbacksIfForAction(entry) ); | ||
} | ||
|
||
return this._callbackList; | ||
}, | ||
|
||
_getCallbacksIfForAction: function(entry){ | ||
var parsedEntry = this._parseEntry(entry); | ||
|
||
if ( | ||
this._actionIsOn(parsedEntry.actions) || | ||
this._allIsOn(parsedEntry.actions) | ||
) | ||
return parsedEntry.callbacks; | ||
|
||
return []; | ||
}, | ||
|
||
_actionIsOn: function(actions){ | ||
return actions.indexOf(this.action) != -1; | ||
}, | ||
|
||
_allIsOn: function(actions){ | ||
return actions.indexOf('all') != -1; | ||
}, | ||
|
||
_parseEntry: function(entry){ | ||
var parts = entry.split('->'), | ||
data = {actions: [], callbacks: []}; | ||
|
||
if (parts[0]) data.actions = parts[0].trim().split(' '); | ||
if (parts[1]) data.callbacks = parts[1].trim().split(' '); | ||
|
||
return data; | ||
} | ||
|
||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
//= require ./init.js | ||
//= require ./base_controller.js | ||
//= require ./controller_builder.js | ||
//= require ./before_callback_performer.js | ||
//= require ./engine.js | ||
//= require ./paloma.js |