diff --git a/src/style_manager/main.js b/src/style_manager/main.js index 4059f2d5a..492fd0d45 100644 --- a/src/style_manager/main.js +++ b/src/style_manager/main.js @@ -173,8 +173,10 @@ define(function(require) { var prop = null; var sector = this.getSector(sectorId); - if(sector) - prop = sectors.get('properties').where({property: name}); + if(sector){ + prop = sector.get('properties').where({property: name}); + prop = prop.length == 1 ? prop[0] : prop; + } return prop; }, @@ -191,7 +193,7 @@ define(function(require) { var sector = this.getSector(sectorId); if(sector) - props = sectors.get('properties'); + props = sector.get('properties'); return props; }, diff --git a/src/style_manager/model/Sector.js b/src/style_manager/model/Sector.js index b800bd34c..a213aa9cb 100644 --- a/src/style_manager/model/Sector.js +++ b/src/style_manager/model/Sector.js @@ -1,5 +1,5 @@ -define(['backbone'], - function(Backbone) { +define(['backbone', './Properties'], + function(Backbone, Properties) { return Backbone.Model.extend({ @@ -8,7 +8,13 @@ define(['backbone'], name: '', open: true, properties : [], - } + }, + + initialize: function(opts) { + var o = opts || {}; + var props = o.properties || this.get('properties'); + this.set('properties', new Properties(props)); + }, }); }); \ No newline at end of file diff --git a/src/style_manager/model/Sectors.js b/src/style_manager/model/Sectors.js index 29b0324be..7c224918f 100644 --- a/src/style_manager/model/Sectors.js +++ b/src/style_manager/model/Sectors.js @@ -1,21 +1,9 @@ -define([ 'backbone','./Sector','./Properties'], - function (Backbone,Sector, Properties) { +define([ 'backbone', './Sector'], + function (Backbone, Sector) { return Backbone.Collection.extend({ model: Sector, - initialize: function(collection) { - - _.each(collection, function(obj){ - - if(obj.properties instanceof Array){ - obj.properties = new Properties(obj.properties); - } - - },this); - - }, - }); }); diff --git a/test/specs/style_manager/main.js b/test/specs/style_manager/main.js index 9fbfbb7ff..eb756f3c8 100644 --- a/test/specs/style_manager/main.js +++ b/test/specs/style_manager/main.js @@ -62,6 +62,114 @@ define([ sect1.should.deep.equal(sect2); }); + it('Add property to inexistent sector', function() { + (obj.addProperty('test', {}) == null).should.equal(true); + }); + + it('Add property', function() { + obj.addSector('test', {}); + (obj.addProperty('test', {}) == null).should.equal(false); + obj.getProperties('test').length.should.equal(1); + }); + + it('Check added property', function() { + obj.addSector('test', {}); + var prop = obj.addProperty('test', { + 'name': 'test', + }); + prop.get('name').should.equal('test'); + }); + + it('Add properties', function() { + obj.addSector('test', {}); + obj.addProperty('test', [{}, {}]); + obj.getProperties('test').length.should.equal(2); + }); + + it('Get property from inexistent sector', function() { + (obj.getProperty('test', 'test-prop') == null).should.equal(true); + }); + + it("Can't get properties without proper name", function() { + obj.addSector('test', {}); + obj.addProperty('test', [{}, {}]); + obj.getProperty('test', 'test-prop').should.be.empty; + }); + + it("Get property with proper name", function() { + obj.addSector('test', {}); + var prop1 = obj.addProperty('test', {property: 'test-prop'}); + var prop2 = obj.getProperty('test', 'test-prop'); + prop1.should.deep.equal(prop2); + }); + + it("Get properties with proper name", function() { + obj.addSector('test', {}); + var prop1 = obj.addProperty('test',[ + {property: 'test-prop'}, + {property: 'test-prop'} + ]); + obj.getProperty('test', 'test-prop').length.should.equal(2); + }); + + it('Get inexistent properties', function() { + (obj.getProperties('test') == null).should.equal(true); + (obj.getProperties() == null).should.equal(true); + }); + + it('Renders correctly', function() { + obj.render().should.be.ok; + }); + + describe('Init with configuration', function() { + + beforeEach(function () { + obj = new StyleManager({ + sectors: [{ + id: 'dim', + name: 'Dimension', + properties: [{ + name: 'Width', + property: 'width', + },{ + name: 'Height', + property: 'height', + }], + },{ + id: 'pos', + name: 'position', + properties: [{ + name: 'Width', + property: 'width', + }], + }], + }); + }); + + afterEach(function () { + delete obj; + }); + + it('Sectors added', function() { + obj.getSectors().length.should.equal(2); + var sect1 = obj.getSector('dim'); + sect1.get('name').should.equal('Dimension'); + }); + + it('Properties added', function() { + var sect1 = obj.getSector('dim'); + var sect2 = obj.getSector('pos'); + sect1.get('properties').length.should.equal(2); + sect2.get('properties').length.should.equal(1); + }); + + it('Property is correct', function() { + var prop1 = obj.getProperty('dim', 'width'); + prop1.get('name').should.equal('Width'); + }); + + }); + }); });