Move preventDefault and stepFalse into Util.
Showing
3 changed files
with
49 additions
and
14 deletions
1 | define(function(require) { | 1 | define(function(require) { |
2 | 'use strict'; | 2 | 'use strict'; |
3 | var Backbone = require('backbone'); | 3 | var Backbone = require('backbone'); |
4 | var Util = require('./Util'); | ||
4 | 5 | ||
5 | function preventDefault(e) { | ||
6 | if (e) { | ||
7 | e.preventDefault(); | ||
8 | } | ||
9 | } | ||
10 | function stepFalse(e) { | ||
11 | preventDefault(e); | ||
12 | return false; | ||
13 | } | ||
14 | var Pagination = Backbone.Model.extend({ | 6 | var Pagination = Backbone.Model.extend({ |
15 | defaults: function defaults() { | 7 | defaults: function defaults() { |
16 | return { | 8 | return { |
... | @@ -120,21 +112,21 @@ define(function(require) { | ... | @@ -120,21 +112,21 @@ define(function(require) { |
120 | var next, previous; | 112 | var next, previous; |
121 | if (hasNext) { | 113 | if (hasNext) { |
122 | next = _.bind(function(e) { | 114 | next = _.bind(function(e) { |
123 | preventDefault(e); | 115 | Util.preventDefault(e); |
124 | this.set('currentPage', this.get('currentPage') + pageCount); | 116 | this.set('currentPage', this.get('currentPage') + pageCount); |
125 | return false; | 117 | return false; |
126 | }, this); | 118 | }, this); |
127 | } else { | 119 | } else { |
128 | next = stepFalse; | 120 | next = Util.stepFalse; |
129 | } | 121 | } |
130 | if (hasPrevious) { | 122 | if (hasPrevious) { |
131 | previous = _.bind(function(e) { | 123 | previous = _.bind(function(e) { |
132 | preventDefault(e); | 124 | Util.preventDefault(e); |
133 | this.set('currentPage', this.get('currentPage') - pageCount); | 125 | this.set('currentPage', this.get('currentPage') - pageCount); |
134 | return false; | 126 | return false; |
135 | }, this); | 127 | }, this); |
136 | } else { | 128 | } else { |
137 | previous = stepFalse; | 129 | previous = Util.stepFalse; |
138 | } | 130 | } |
139 | this.set(nextName, next); | 131 | this.set(nextName, next); |
140 | this.set(previousName, previous); | 132 | this.set(previousName, previous); | ... | ... |
... | @@ -14,7 +14,17 @@ define(function(require) { | ... | @@ -14,7 +14,17 @@ define(function(require) { |
14 | return result; | 14 | return result; |
15 | } | 15 | } |
16 | 16 | ||
17 | function preventDefault(e) { | ||
18 | if (e) { | ||
19 | e.preventDefault(); | ||
20 | } | ||
21 | } | ||
17 | return { | 22 | return { |
23 | preventDefault: preventDefault, | ||
24 | stepFalse: function stepFalse(e) { | ||
25 | preventDefault(e); | ||
26 | return false; | ||
27 | }, | ||
18 | getField: function getField(obj, key) { | 28 | getField: function getField(obj, key) { |
19 | return obj[key]; | 29 | return obj[key]; |
20 | }, | 30 | }, | ... | ... |
... | @@ -33,6 +33,8 @@ define(function(require) { | ... | @@ -33,6 +33,8 @@ define(function(require) { |
33 | keys[key] = true; | 33 | keys[key] = true; |
34 | }); | 34 | }); |
35 | expect(keys).toEqual({ | 35 | expect(keys).toEqual({ |
36 | preventDefault: true, | ||
37 | stepFalse: true, | ||
36 | getField: true, | 38 | getField: true, |
37 | setField: true, | 39 | setField: true, |
38 | getItemKeyAccessor: true, | 40 | getItemKeyAccessor: true, |
... | @@ -41,11 +43,42 @@ define(function(require) { | ... | @@ -41,11 +43,42 @@ define(function(require) { |
41 | }); | 43 | }); |
42 | }); | 44 | }); |
43 | describe('methods', function() { | 45 | describe('methods', function() { |
44 | var pojo, bb, emptyBB; | 46 | var e, pojo, bb, emptyBB; |
45 | beforeEach(function() { | 47 | beforeEach(function() { |
46 | pojo = {a: 1, b: 2, key: 'TheKey'}; | 48 | pojo = {a: 1, b: 2, key: 'TheKey'}; |
47 | bb = new BaseModel(pojo); | 49 | bb = new BaseModel(pojo); |
48 | emptyBB = new SubModel(); | 50 | emptyBB = new SubModel(); |
51 | e = {preventDefault: jasmine.createSpy('preventDefault')}; | ||
52 | }); | ||
53 | it('preventDefault(null)', function() { | ||
54 | var r = Util.preventDefault(null); | ||
55 | expect(r).toBe(undefined); | ||
56 | expect(e.preventDefault).not.toHaveBeenCalled(); | ||
57 | }); | ||
58 | it('preventDefault(undefined)', function() { | ||
59 | var r = Util.preventDefault(undefined); | ||
60 | expect(r).toBe(undefined); | ||
61 | expect(e.preventDefault).not.toHaveBeenCalled(); | ||
62 | }); | ||
63 | it('preventDefault(e)', function() { | ||
64 | var r = Util.preventDefault(e); | ||
65 | expect(r).toBe(undefined); | ||
66 | expect(e.preventDefault).toHaveBeenCalled(); | ||
67 | }); | ||
68 | it('stepFalse(null)', function() { | ||
69 | var r = Util.stepFalse(null); | ||
70 | expect(r).toBe(false); | ||
71 | expect(e.preventDefault).not.toHaveBeenCalled(); | ||
72 | }); | ||
73 | it('stepFalse(undefined)', function() { | ||
74 | var r = Util.stepFalse(undefined); | ||
75 | expect(r).toBe(false); | ||
76 | expect(e.preventDefault).not.toHaveBeenCalled(); | ||
77 | }); | ||
78 | it('preventDefault(e)', function() { | ||
79 | var r = Util.stepFalse(e); | ||
80 | expect(r).toBe(false); | ||
81 | expect(e.preventDefault).toHaveBeenCalled(); | ||
49 | }); | 82 | }); |
50 | it('setField only modifies given', function() { | 83 | it('setField only modifies given', function() { |
51 | var r = Util.setField(pojo, 'a', 100); | 84 | var r = Util.setField(pojo, 'a', 100); | ... | ... |
-
Please register or sign in to post a comment