First set of sharable backbone models; they don't do a whole lot yet.
Showing
22 changed files
with
537 additions
and
0 deletions
Gruntfile.js
0 → 100644
This diff is collapsed.
Click to expand it.
bower.json
0 → 100644
1 | { | ||
2 | "name": "ofbiz-shared", | ||
3 | "version": "0.0.0", | ||
4 | "authors": [ | ||
5 | "Adam Heath <doogie@brainfood.com>" | ||
6 | ], | ||
7 | "private": true, | ||
8 | "ignore": [ | ||
9 | "**/.*", | ||
10 | "node_modules", | ||
11 | "src/lib", | ||
12 | "test" | ||
13 | ], | ||
14 | "dependencies": { | ||
15 | "backbone": "~1.1.0", | ||
16 | "backbone-validation": "0.9.1", | ||
17 | "requirejs": "~2.1.10" | ||
18 | } | ||
19 | } |
package.json
0 → 100644
1 | { | ||
2 | "name": "ofbiz-shared", | ||
3 | "version": "0.0.0", | ||
4 | "_main": [ | ||
5 | "dist/scripts/bundles/ofbiz-Base.js", | ||
6 | "dist/scripts/bundles/ofbiz-Party.js", | ||
7 | "dist/scripts/bundles/ofbiz-ContactMechs.js" | ||
8 | ], | ||
9 | "dependencies": { | ||
10 | "backbone": "~1.1.0", | ||
11 | "backbone-validation": "0.9.1", | ||
12 | "requirejs": "~2.1.10" | ||
13 | }, | ||
14 | "devDependencies": { | ||
15 | "bower-requirejs": "~0.9.2", | ||
16 | "grunt": "~0.4.1", | ||
17 | "grunt-contrib-copy": "~0.4.1", | ||
18 | "grunt-contrib-concat": "~0.3.0", | ||
19 | "grunt-contrib-uglify": "~0.2.0", | ||
20 | "grunt-contrib-jshint": "~0.7.0", | ||
21 | "grunt-contrib-cssmin": "~0.7.0", | ||
22 | "grunt-contrib-connect": "~0.5.0", | ||
23 | "grunt-contrib-clean": "~0.5.0", | ||
24 | "grunt-contrib-htmlmin": "~0.1.3", | ||
25 | "grunt-bower-install": "~0.7.0", | ||
26 | "grunt-contrib-imagemin": "~0.2.0", | ||
27 | "grunt-contrib-watch": "~0.5.2", | ||
28 | "grunt-rev": "~0.1.0", | ||
29 | "grunt-autoprefixer": "~0.5.0", | ||
30 | "grunt-usemin": "~0.1.10", | ||
31 | "grunt-mocha": "~0.4.0", | ||
32 | "grunt-newer": "~0.6.0", | ||
33 | "grunt-svgmin": "~0.2.0", | ||
34 | "grunt-concurrent": "~0.4.0", | ||
35 | "load-grunt-tasks": "~0.2.0", | ||
36 | "time-grunt": "~0.2.0", | ||
37 | "jshint-stylish": "~0.1.3", | ||
38 | "grunt-contrib-requirejs": "~0.4.0", | ||
39 | "grunt-bower-requirejs": "~0.8.4", | ||
40 | "grunt-template-jasmine-istanbul": "~0.2.6", | ||
41 | "grunt-template-jasmine-requirejs": "~0.1.10", | ||
42 | "grunt-contrib-jasmine": "~0.5.3" | ||
43 | }, | ||
44 | "engines": { | ||
45 | "node": ">=0.8.0" | ||
46 | } | ||
47 | } | ||
48 |
src/index.html
0 → 100644
1 | <!doctype html> | ||
2 | <html class="no-js"> | ||
3 | <head> | ||
4 | <meta charset="utf-8"> | ||
5 | <title>ofbiz shared</title> | ||
6 | <meta name="description" content=""> | ||
7 | <!-- Place favicon.ico and apple-touch-icon.png in the root directory --> | ||
8 | |||
9 | <!-- build:css({src, .tmp}) styles/main.css --> | ||
10 | <link rel="stylesheet" href="styles/main.css"> | ||
11 | <!-- endbuild --> | ||
12 | </head> | ||
13 | <body> | ||
14 | <script src="scripts/config.js"></script> | ||
15 | <!-- build:js({src,.tmp}) scripts/require.js --> | ||
16 | <script src="scripts/bundles.js"></script> | ||
17 | <script src="lib/requirejs/require.js"></script> | ||
18 | <!-- endbuild --> | ||
19 | <script src="scripts/main.js"></script> | ||
20 | </body> | ||
21 | </html> |
src/scripts/config.js
0 → 100644
1 | /* global require:true */ | ||
2 | var require; | ||
3 | require = (function() { | ||
4 | 'use strict'; | ||
5 | |||
6 | var require = { | ||
7 | shim: { | ||
8 | |||
9 | }, | ||
10 | paths: { | ||
11 | underscore: '../lib/underscore/underscore', | ||
12 | 'backbone-validation': '../lib/backbone-validation/dist/backbone-validation-amd', | ||
13 | backbone: '../lib/backbone/backbone' | ||
14 | } | ||
15 | }; | ||
16 | |||
17 | return require; | ||
18 | })(); |
src/scripts/jquery.js
0 → 100644
1 | define([], {}); |
src/scripts/main.js
0 → 100644
src/scripts/ofbiz/model/Base.js
0 → 100644
1 | define( | ||
2 | [], | ||
3 | function() { | ||
4 | /* global console */ | ||
5 | 'use strict'; | ||
6 | console.log('ofbiz/model/Base: loaded'); | ||
7 | var Base = { | ||
8 | createRequiredChecker: function(label) { | ||
9 | return function() { | ||
10 | console.log(label + ':required'); | ||
11 | return this._isRequired; | ||
12 | }; | ||
13 | }, | ||
14 | createRequiredValidator: function(label) { | ||
15 | return { | ||
16 | required: Base.createRequiredChecker(), | ||
17 | }; | ||
18 | }, | ||
19 | }; | ||
20 | return Base; | ||
21 | } | ||
22 | ); |
src/scripts/ofbiz/model/ContactMech.js
0 → 100644
src/scripts/ofbiz/model/EmailAddress.js
0 → 100644
1 | /* global console */ | ||
2 | define( | ||
3 | [ | ||
4 | 'underscore', | ||
5 | 'ofbiz/model/Base', | ||
6 | 'ofbiz/model/ContactMech', | ||
7 | ], | ||
8 | function( | ||
9 | _, | ||
10 | Base, | ||
11 | ContactMech | ||
12 | ) { | ||
13 | 'use strict'; | ||
14 | console.log('ofbiz/model/EmailAddress: loaded'); | ||
15 | return ContactMech.extend({ | ||
16 | defaults: { | ||
17 | emailAddress: null, | ||
18 | }, | ||
19 | validation: { | ||
20 | emailAddress: [ | ||
21 | { | ||
22 | pattern: 'email', | ||
23 | required: Base.createRequiredChecker('ofbiz/model/EmailAddress.emailAddress'), | ||
24 | }, | ||
25 | ] | ||
26 | }, | ||
27 | parse: function(data) { | ||
28 | if (data.infoString !== undefined) { | ||
29 | data = _.clone(data); | ||
30 | if (data.emailAddress === undefined) { | ||
31 | data.emailAddress = data.infoString; | ||
32 | } | ||
33 | delete data.infoString; | ||
34 | } | ||
35 | return data; | ||
36 | }, | ||
37 | }); | ||
38 | } | ||
39 | ); |
src/scripts/ofbiz/model/Party.js
0 → 100644
1 | /* global console */ | ||
2 | define( | ||
3 | [ | ||
4 | 'backbone', | ||
5 | ], | ||
6 | function( | ||
7 | Backbone | ||
8 | ) { | ||
9 | 'use strict'; | ||
10 | console.log('ofbiz/model/Party: loaded'); | ||
11 | return Backbone.Model.extend({ | ||
12 | idAttribute: 'partyId', | ||
13 | defaults: { | ||
14 | partyId: null, | ||
15 | }, | ||
16 | validation: { | ||
17 | partyId: null, | ||
18 | } | ||
19 | }); | ||
20 | } | ||
21 | ); |
src/scripts/ofbiz/model/PartyGroup.js
0 → 100644
1 | /* global console */ | ||
2 | define( | ||
3 | [ | ||
4 | 'ofbiz/model/Base', | ||
5 | 'ofbiz/model/Party', | ||
6 | ], | ||
7 | function( | ||
8 | Base, | ||
9 | Party | ||
10 | ) { | ||
11 | 'use strict'; | ||
12 | console.log('ofbiz/model/PartyGroup: loaded'); | ||
13 | return Party.extend({ | ||
14 | defaults: { | ||
15 | groupName: null, | ||
16 | }, | ||
17 | validation: { | ||
18 | groupName: [ | ||
19 | Base.createRequiredValidator('ofbiz/model/PartyGroup.groupName'), | ||
20 | ], | ||
21 | }, | ||
22 | }); | ||
23 | } | ||
24 | ); |
src/scripts/ofbiz/model/Person.js
0 → 100644
1 | /* global console */ | ||
2 | define( | ||
3 | [ | ||
4 | 'ofbiz/model/Base', | ||
5 | 'ofbiz/model/Party', | ||
6 | ], | ||
7 | function( | ||
8 | Base, | ||
9 | Party | ||
10 | ) { | ||
11 | 'use strict'; | ||
12 | console.log('ofbiz/model/Person: loaded'); | ||
13 | return Party.extend({ | ||
14 | defaults: { | ||
15 | firstName: null, | ||
16 | lastName: null, | ||
17 | }, | ||
18 | validation: { | ||
19 | firstName: [ | ||
20 | Base.createRequiredValidator('ofbiz/model/Person.firstName'), | ||
21 | ], | ||
22 | lastName: [ | ||
23 | Base.createRequiredValidator('ofbiz/model/Person.lastName'), | ||
24 | ], | ||
25 | }, | ||
26 | }); | ||
27 | } | ||
28 | ); |
src/scripts/ofbiz/model/PostalAddress.js
0 → 100644
1 | /* global console */ | ||
2 | define( | ||
3 | [ | ||
4 | 'ofbiz/model/Base', | ||
5 | 'ofbiz/model/ContactMech', | ||
6 | ], | ||
7 | function( | ||
8 | Base, | ||
9 | ContactMech | ||
10 | ) { | ||
11 | 'use strict'; | ||
12 | console.log('ofbiz/model/PostalAddress: loaded'); | ||
13 | return ContactMech.extend({ | ||
14 | defaults: { | ||
15 | address1: null, | ||
16 | city: null, | ||
17 | postalCode: null, | ||
18 | }, | ||
19 | validation: { | ||
20 | address1: [ | ||
21 | Base.createRequiredValidator('ofbiz/model/PostalAddress.address1'), | ||
22 | ], | ||
23 | city: [ | ||
24 | Base.createRequiredValidator('ofbiz/model/PostalAddress.city'), | ||
25 | ], | ||
26 | postalCode: [ | ||
27 | Base.createRequiredValidator('ofbiz/model/PostalAddress.postalCode'), | ||
28 | ], | ||
29 | } | ||
30 | }); | ||
31 | } | ||
32 | ); |
src/scripts/ofbiz/model/TelecomNumber.js
0 → 100644
1 | /* global console */ | ||
2 | define( | ||
3 | [ | ||
4 | 'ofbiz/model/Base', | ||
5 | 'ofbiz/model/ContactMech', | ||
6 | ], | ||
7 | function( | ||
8 | Base, | ||
9 | ContactMech | ||
10 | ) { | ||
11 | 'use strict'; | ||
12 | console.log('ofbiz/model/TelecomNumber: loaded'); | ||
13 | return ContactMech.extend({ | ||
14 | defaults: { | ||
15 | contactNumber: null, | ||
16 | }, | ||
17 | validation: { | ||
18 | contactNumber: [ | ||
19 | Base.createRequiredValidator('ofbiz/model/TelecomNumber.contactNumber'), | ||
20 | ] | ||
21 | } | ||
22 | }); | ||
23 | } | ||
24 | ); |
src/styles/main.css
0 → 100644
File mode changed
test/specs/ofbiz/model/Base.spec.js
0 → 100644
1 | define(function(require) { | ||
2 | 'use strict'; | ||
3 | |||
4 | var Base = require('ofbiz/model/Base'); | ||
5 | var Backbone = require('backbone'); | ||
6 | |||
7 | describe('Base', function() { | ||
8 | it('exists', function() { | ||
9 | expect(Base).toBeTruthy(); | ||
10 | }); | ||
11 | }); | ||
12 | describe('Base', function() { | ||
13 | it('_createRequiredValidator', function() { | ||
14 | var TestModel = Backbone.Model.extend({ | ||
15 | required: function() { | ||
16 | return this.validators.all[0].required.call(this, arguments); | ||
17 | }, | ||
18 | validators: { | ||
19 | all: [ | ||
20 | Base.createRequiredValidator('jasmine test'), | ||
21 | ], | ||
22 | }, | ||
23 | }); | ||
24 | var testModel = new TestModel(); | ||
25 | expect(testModel.required()).toBe(undefined); | ||
26 | testModel._isRequired = false; | ||
27 | expect(testModel.required()).toBe(false); | ||
28 | testModel._isRequired = true; | ||
29 | expect(testModel.required()).toBe(true); | ||
30 | }); | ||
31 | }); | ||
32 | }); |
test/specs/ofbiz/model/EmailAddress.spec.js
0 → 100644
1 | define(function(require) { | ||
2 | 'use strict'; | ||
3 | |||
4 | var EmailAddress= require('ofbiz/model/EmailAddress'); | ||
5 | var _ = require('underscore'); | ||
6 | var Backbone = require('backbone'); | ||
7 | require('backbone-validation'); | ||
8 | |||
9 | describe('EmailAddress', function() { | ||
10 | it('exists', function() { | ||
11 | expect(EmailAddress).toBeTruthy(); | ||
12 | }); | ||
13 | }); | ||
14 | describe('EmailAddress', function() { | ||
15 | beforeEach(function() { | ||
16 | _.extend(Backbone.Model.prototype, Backbone.Validation.mixin); | ||
17 | }); | ||
18 | it('validation', function() { | ||
19 | var result, emailAddress; | ||
20 | emailAddress = new EmailAddress(); | ||
21 | expect(emailAddress.validate()).toBeUndefined(); | ||
22 | expect(emailAddress.isValid()).toBe(true); | ||
23 | |||
24 | emailAddress._isRequired = true; | ||
25 | // changing _isRequired doesn't have an effect until validate is called | ||
26 | expect(emailAddress.isValid()).toBe(true); | ||
27 | result = emailAddress.validate(); | ||
28 | expect(result).not.toBeUndefined(); | ||
29 | expect(emailAddress.isValid()).toBe(false); | ||
30 | expect(_.keys(result).sort()).toEqual(['emailAddress']); | ||
31 | expect(result['emailAddress']).toEqual(jasmine.any(String)); | ||
32 | |||
33 | emailAddress.set('emailAddress', 'foo'); | ||
34 | result = emailAddress.validate(); | ||
35 | expect(result).not.toBeUndefined(); | ||
36 | expect(emailAddress.isValid()).toBe(false); | ||
37 | expect(_.keys(result).sort()).toEqual(['emailAddress']); | ||
38 | expect(result['emailAddress']).toEqual(jasmine.any(String)); | ||
39 | |||
40 | emailAddress.set('emailAddress', 'name@example.com'); | ||
41 | result = emailAddress.validate(); | ||
42 | expect(result).toBeUndefined(); | ||
43 | expect(emailAddress.isValid()).toBe(true); | ||
44 | |||
45 | |||
46 | emailAddress.set('emailAddress', null); | ||
47 | result = emailAddress.validate(); | ||
48 | expect(result).not.toBeUndefined(); | ||
49 | expect(emailAddress.isValid()).toBe(false); | ||
50 | expect(_.keys(result).sort()).toEqual(['emailAddress']); | ||
51 | expect(result['emailAddress']).toEqual(jasmine.any(String)); | ||
52 | |||
53 | emailAddress._isRequired = false; | ||
54 | expect(emailAddress.isValid()).toBe(false); | ||
55 | expect(emailAddress.validate()).toBeUndefined(); | ||
56 | expect(emailAddress.isValid()).toBe(true); | ||
57 | }); | ||
58 | it('parse', function() { | ||
59 | var result, emailAddress; | ||
60 | emailAddress = new EmailAddress({infoString: 'name@example.com'}, {parse: true}); | ||
61 | expect(emailAddress.get('infoString')).toBeUndefined(); | ||
62 | expect(emailAddress.get('emailAddress')).toEqual('name@example.com'); | ||
63 | |||
64 | emailAddress = new EmailAddress({emailAddress: 'name@example.com'}, {parse: true}); | ||
65 | expect(emailAddress.get('infoString')).toBeUndefined(); | ||
66 | expect(emailAddress.get('emailAddress')).toEqual('name@example.com'); | ||
67 | |||
68 | emailAddress = new EmailAddress({infoString: 'foo', emailAddress: 'name@example.com'}, {parse: true}); | ||
69 | expect(emailAddress.get('infoString')).toBeUndefined(); | ||
70 | expect(emailAddress.get('emailAddress')).toEqual('name@example.com'); | ||
71 | }); | ||
72 | }); | ||
73 | }); |
test/specs/ofbiz/model/PostalAddress.spec.js
0 → 100644
1 | define(function(require) { | ||
2 | 'use strict'; | ||
3 | |||
4 | var PostalAddress= require('ofbiz/model/PostalAddress'); | ||
5 | var _ = require('underscore'); | ||
6 | var Backbone = require('backbone'); | ||
7 | require('backbone-validation'); | ||
8 | |||
9 | describe('PostalAddress', function() { | ||
10 | it('exists', function() { | ||
11 | expect(PostalAddress).toBeTruthy(); | ||
12 | }); | ||
13 | }); | ||
14 | if (true) describe('PostalAddress', function() { | ||
15 | beforeEach(function() { | ||
16 | _.extend(Backbone.Model.prototype, Backbone.Validation.mixin); | ||
17 | }); | ||
18 | it('validation', function() { | ||
19 | var result, postalAddress; | ||
20 | postalAddress = new PostalAddress(); | ||
21 | expect(postalAddress.validate()).toBeUndefined(); | ||
22 | expect(postalAddress.isValid()).toBe(true); | ||
23 | |||
24 | postalAddress._isRequired = true; | ||
25 | // changing _isRequired doesn't have an effect until validate is called | ||
26 | expect(postalAddress.isValid()).toBe(true); | ||
27 | result = postalAddress.validate() | ||
28 | expect(result).not.toBeUndefined(); | ||
29 | expect(postalAddress.isValid()).toBe(false); | ||
30 | expect(_.keys(result).sort()).toEqual(['address1', 'city', 'postalCode']); | ||
31 | expect(result['address1']).toEqual(jasmine.any(String)); | ||
32 | expect(result['city']).toEqual(jasmine.any(String)); | ||
33 | expect(result['postalCode']).toEqual(jasmine.any(String)); | ||
34 | |||
35 | postalAddress.set('city', 'Anytown'); | ||
36 | result = postalAddress.validate() | ||
37 | expect(result).not.toBeUndefined(); | ||
38 | expect(postalAddress.isValid()).toBe(false); | ||
39 | expect(_.keys(result).sort()).toEqual(['address1', 'postalCode']); | ||
40 | expect(result['address1']).toEqual(jasmine.any(String)); | ||
41 | expect(result['postalCode']).toEqual(jasmine.any(String)); | ||
42 | |||
43 | |||
44 | postalAddress._isRequired = false; | ||
45 | expect(postalAddress.isValid()).toBe(false); | ||
46 | expect(postalAddress.validate()).toBeUndefined(); | ||
47 | expect(postalAddress.isValid()).toBe(true); | ||
48 | }); | ||
49 | }); | ||
50 | }); |
test/specs/ofbiz/model/TelecomNumber.spec.js
0 → 100644
1 | define(function(require) { | ||
2 | 'use strict'; | ||
3 | |||
4 | var TelecomNumber = require('ofbiz/model/TelecomNumber'); | ||
5 | var _ = require('underscore'); | ||
6 | var Backbone = require('backbone'); | ||
7 | require('backbone-validation'); | ||
8 | |||
9 | describe('TelecomNumber', function() { | ||
10 | it('exists', function() { | ||
11 | expect(TelecomNumber).toBeTruthy(); | ||
12 | }); | ||
13 | }); | ||
14 | if (true) describe('TelecomNumber', function() { | ||
15 | beforeEach(function() { | ||
16 | _.extend(Backbone.Model.prototype, Backbone.Validation.mixin); | ||
17 | }); | ||
18 | it('validation', function() { | ||
19 | var result, telecomNumber; | ||
20 | telecomNumber = new TelecomNumber(); | ||
21 | expect(telecomNumber.validate()).toBeUndefined(); | ||
22 | expect(telecomNumber.isValid()).toBe(true); | ||
23 | |||
24 | telecomNumber._isRequired = true; | ||
25 | // changing _isRequired doesn't have an effect until validate is called | ||
26 | expect(telecomNumber.isValid()).toBe(true); | ||
27 | result = telecomNumber.validate() | ||
28 | expect(result).not.toBeUndefined(); | ||
29 | expect(telecomNumber.isValid()).toBe(false); | ||
30 | expect(_.keys(result).sort()).toEqual(['contactNumber']); | ||
31 | expect(result['contactNumber']).toEqual(jasmine.any(String)); | ||
32 | |||
33 | telecomNumber.set('contactNumber', '123-456-7890'); | ||
34 | result = telecomNumber.validate() | ||
35 | expect(result).toBeUndefined(); | ||
36 | expect(telecomNumber.isValid()).toBe(true); | ||
37 | |||
38 | telecomNumber._isRequired = false; | ||
39 | expect(telecomNumber.isValid()).toBe(true); | ||
40 | expect(telecomNumber.validate()).toBeUndefined(); | ||
41 | expect(telecomNumber.isValid()).toBe(true); | ||
42 | }); | ||
43 | }); | ||
44 | }); |
-
Please register or sign in to post a comment