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

Minor fix to registry #86

Merged
merged 1 commit into from
Feb 28, 2015
Merged
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
4 changes: 2 additions & 2 deletions spec/javascripts/inputReaders.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('input readers', function() {
});

afterEach(function() {
Backbone.Syphon.InputReaders.register('text');
Backbone.Syphon.InputReaders.unregister('text');
});

it('should retrieve the registered "text" reader', function() {
Expand All @@ -39,7 +39,7 @@ describe('input readers', function() {
});

afterEach(function() {
Backbone.Syphon.InputReaders.register('textarea');
Backbone.Syphon.InputReaders.unregister('textarea');
});

it('should be able to retrieve the input reader for that type', function() {
Expand Down
61 changes: 61 additions & 0 deletions spec/javascripts/typeregistry.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
describe('Type Registry', function () {
var typeRegistry;
beforeEach(function () {
typeRegistry = new Backbone.Syphon.TypeRegistry();
});

it('should register a default item', function () {
var item = {};
typeRegistry.registerDefault(item);
expect(typeRegistry.get('default')).to.be.eql(item);
expect(typeRegistry.get()).to.be.eql(item);
expect(typeRegistry.get('some type that doesnt exist')).to.be.eql(item);
});

it('should get an item', function () {
var item = {};
typeRegistry.register('foo', item);
expect(typeRegistry.get('foo')).to.be.eql(item);
});

it('should not get a non existing item', function () {
var item = {};
typeRegistry.register('foo', item);
expect(typeRegistry.get('moo')).to.be.undefined;
});

it('should register an item', function () {
var item = 10;
typeRegistry.register('foo', item);
expect(typeRegistry.get('foo')).to.be.equal(item);
});

it('should register a falsy item', function () {
var item = null;
typeRegistry.register('foo', item);
expect(typeRegistry.get('foo')).to.be.equal(item);
});

it('should re-register and item', function () {
var item1 = {};
var item2 = {};
typeRegistry.register('foo', item1);
typeRegistry.register('foo', item2);
expect(typeRegistry.get('foo')).to.be.eql(item2);
});

it('should unregister a type', function () {
var item = {};
typeRegistry.register('foo', item);
typeRegistry.unregister('foo');
expect(typeRegistry.get('foo')).to.be.undefined;
});

it('should unregister a falsy value', function () {
typeRegistry.register('foo', null);
typeRegistry.unregister('foo');
expect(typeRegistry.get('foo')).to.be.undefined;
});


});
18 changes: 11 additions & 7 deletions src/backbone.syphon.typeregistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Type Registries allow you to register something to
// an input type, and retrieve either the item registered
// for a specific type or the default registration
var TypeRegistry = Syphon.TypeRegistry = function() {
var TypeRegistry = Syphon.TypeRegistry = function () {
this.registeredTypes = {};
};

Expand All @@ -16,24 +16,28 @@ _.extend(TypeRegistry.prototype, {
// Get the registered item by type. If nothing is
// found for the specified type, the default is
// returned.
get: function(type){
return this.registeredTypes[type] || this.registeredTypes['default'];
get: function (type) {
if (_.has(this.registeredTypes, type)) {
return this.registeredTypes[type];
} else {
return this.registeredTypes['default'];
}
},

// Register a new item for a specified type
register: function(type, item) {
register: function (type, item) {
this.registeredTypes[type] = item;
},

// Register a default item to be used when no
// item for a specified type is found
registerDefault: function(item) {
registerDefault: function (item) {
this.registeredTypes['default'] = item;
},

// Remove an item from a given type registration
unregister: function(type) {
if (this.registeredTypes[type]) {
unregister: function (type) {
if (_.has(this.registeredTypes, type)) {
delete this.registeredTypes[type];
}
}
Expand Down