Skip to content

Instantly share code, notes, and snippets.

@andrewk

andrewk/count.js Secret

Created July 23, 2014 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewk/c9a3a431624e910d9985 to your computer and use it in GitHub Desktop.
Save andrewk/c9a3a431624e910d9985 to your computer and use it in GitHub Desktop.
Count Component
/**
* Interface for a count, affected by remote addition or subtraction
* event should send data with 'property' property, set to eg: -1 to lower by one
*/
define(function(require) {
'use strict';
var defineComponent = require('flight/lib/component');
function Count() {
this.defaultAttrs({
'event': null
});
this.after('initialize', function() {
this.on(document, this.attr.event, this.update.bind(this));
});
this.update = function(event, data) {
this.$node.text(
parseInt(this.$node.text(), 10) + data.modifier
);
}
}
return defineComponent(Count);
});
describeComponent('component_ui/count', function () {
beforeEach(function () {
setupComponent('<span>8</span>', {'event': 'uiChangeCount'});
});
describe('receiving event', function() {
it('applies a positive modifier', function() {
$(document).trigger('uiChangeCount', {'modifier': 3});
expect(this.component.$node).toHaveText('11');
});
it('applies a negative modifier', function() {
$(document).trigger('uiChangeCount', {'modifier': -1});
expect(this.component.$node).toHaveText('7');
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment