Skip to content

Commit 7444540

Browse files
author
Maciej Gurban
committed
Refactored 'changed' method and its usage
Also: - Visibility divs not appended on DOM ready - Docs updated accordingly
1 parent 3353c5c commit 7444540

File tree

5 files changed

+53
-62
lines changed

5 files changed

+53
-62
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
### Changelog
22

3+
**2.4.2**
4+
Refactoring 'changed' method (and updating usage examples in main.js) to solve issue [#14](https://github.com/maciej-gurban/responsive-bootstrap-toolkit/issues/14).
5+
6+
Delaying the injection of visibility divs into `<body>` until `$(document).ready()`, and thus allowing the inclusion of library inside `<head>` section.
7+
38
**2.4.1**
49

510
Updating Bootstrap visibility classes for future compliancy, updating documentation to reflect changes in version 2.4.0, and small code refactoring.

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Responsive Bootstrap Toolkit provides an easy way of breakpoint detection in Jav
55

66
The SASS module enables quick and simple styling for elements needing different property values for each screen resolution.
77

8-
Current version: **2.4.1**
8+
Current version: **2.4.2**
99

1010
### Table of Contents
1111
* [Installation](#installation)
@@ -34,7 +34,7 @@ Available on [CodePen](http://codepen.io/dih/full/ivECj)
3434

3535
### Migrating from an older version
3636

37-
Refer to the [changelog](https://github.com/maciej-gurban/responsive-bootstrap-toolkit/blob/master/CHANGELOG.md) for a list of improvements in each version of the library.
37+
Refer to the [changelog](https://github.com/maciej-gurban/responsive-bootstrap-toolkit/blob/master/CHANGELOG.md) for a list of changes in each version of the library.
3838

3939
### JavaScript features
4040
#### Basic usage:
@@ -59,12 +59,12 @@ Refer to the [changelog](https://github.com/maciej-gurban/responsive-bootstrap-t
5959
}
6060

6161
// Execute code each time window size changes
62-
$(window).bind('resize', function() {
62+
$(window).resize(
6363
viewport.changed(function(){
6464
if( viewport.is('xs') ) {
6565
// ...
6666
}
67-
});
67+
})
6868
});
6969

7070
})(jQuery, ResponsiveBootstrapToolkit);
@@ -74,8 +74,8 @@ Refer to the [changelog](https://github.com/maciej-gurban/responsive-bootstrap-t
7474
Allows using custom debounce interval. The default one is set at 300ms.
7575

7676
````javascript
77-
$(window).bind('resize', function() {
78-
viewport.changed(function() {
77+
$(window).resize(
78+
viewport.changed(function(){
7979

8080
// ...
8181

@@ -85,8 +85,8 @@ $(window).bind('resize', function() {
8585

8686
#### Get alias of current breakpoint
8787
````javascript
88-
$(window).bind('resize', function() {
89-
viewport.changed(function() {
88+
$(window).resize(
89+
viewport.changed(function(){
9090

9191
console.log( 'Current breakpoint: '+ viewport.current() );
9292

js/bootstrap-toolkit.js

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Responsive Bootstrap Toolkit
33
* Author: Maciej Gurban
44
* License: MIT
5-
* Version: 2.4.1 (2015-04-19)
5+
* Version: 2.4.2 (2015-05-09)
66
* Origin: https://github.com/maciej-gurban/responsive-bootstrap-toolkit
77
*/
88
;var ResponsiveBootstrapToolkit = (function($){
@@ -24,7 +24,7 @@
2424

2525
// Used operator
2626
var operator = str.charAt(0);
27-
// Include breakpoint equal to alias?
27+
// Include breakpoint equal to alias?
2828
var orEqual = (str.charAt(1) == '=') ? true : false
2929

3030
/**
@@ -54,7 +54,7 @@
5454
isAnyActive: function( breakpoints ) {
5555
var found = false;
5656
$.each(breakpoints, function( index, alias ) {
57-
// Once first breakpoint matches, return true and break the out of the loop
57+
// Once first breakpoint matches, return true and break out of the loop
5858
if( self.breakpoints[ alias ].is(':visible') ) {
5959
found = true;
6060
return false;
@@ -87,7 +87,7 @@
8787
* at 'md' breakpoint, indicated in the expression,
8888
* That makes: start = 0, end = 2 (index of 'md' breakpoint)
8989
*
90-
* Parsing viewport.is('<md') we start at index 'xs' breakpoint, and end at
90+
* Parsing viewport.is('<md') we start at index 'xs' breakpoint, and end at
9191
* 'sm' breakpoint, one before 'md'.
9292
* Which makes: start = 0, end = 1
9393
*/
@@ -100,7 +100,7 @@
100100
* of breakpoint list.
101101
* That makes: start = 1, end = undefined
102102
*
103-
* Parsing viewport.is('>sm') we start at breakpoint 'md' and end at the end of
103+
* Parsing viewport.is('>sm') we start at breakpoint 'md' and end at the end of
104104
* breakpoint list.
105105
* Which makes: start = 2, end = undefined
106106
*/
@@ -111,7 +111,7 @@
111111

112112
var acceptedBreakpoints = breakpointList.slice(start, end);
113113

114-
return internal.isAnyActive( acceptedBreakpoints );
114+
return internal.isAnyActive( acceptedBreakpoints );
115115

116116
}
117117
}
@@ -122,27 +122,20 @@
122122
var self = {
123123

124124
/**
125-
* Determines default interval between firing 'changed' method
125+
* Determines default debouncing interval of 'changed' method
126126
*/
127127
interval: 300,
128128

129129
/**
130130
* Breakpoint aliases, listed from smallest to biggest
131131
*/
132132
breakpoints: {
133-
'xs': $('<div class="device-xs visible-xs visible-xs-block"></div>').appendTo('body'),
134-
'sm': $('<div class="device-sm visible-sm visible-sm-block"></div>').appendTo('body'),
135-
'md': $('<div class="device-md visible-md visible-md-block"></div>').appendTo('body'),
136-
'lg': $('<div class="device-lg visible-lg visible-lg-block"></div>').appendTo('body')
133+
'xs': $.parseHTML('<div class="device-xs visible-xs visible-xs-block"></div>'),
134+
'sm': $.parseHTML('<div class="device-sm visible-sm visible-sm-block"></div>'),
135+
'md': $.parseHTML('<div class="device-md visible-md visible-md-block"></div>'),
136+
'lg': $.parseHTML('<div class="device-lg visible-lg visible-lg-block"></div>')
137137
},
138138

139-
/**
140-
* Debouncing helper
141-
*
142-
* Used to calculate intervals between consecutive function executions
143-
*/
144-
timer: new Date(),
145-
146139
/**
147140
* Returns true if current breakpoint matches passed alias
148141
*/
@@ -170,27 +163,27 @@
170163

171164
/*
172165
* Waits specified number of miliseconds before executing a callback
173-
* Source: http://stackoverflow.com/a/4541963/2066118
174166
*/
175-
changed: function() {
176-
var timers = {};
177-
return function(callback, ms) {
178-
// Get unique timer ID
179-
var uID = (!uID) ? self.timer.getTime() : null;
180-
181-
if (timers[uID]) {
182-
clearTimeout(timers[uID]);
183-
}
184-
185-
// Uses default interval if none specified
186-
if (typeof ms === "undefined") {
187-
var ms = self.interval;
188-
}
189-
timers[uID] = setTimeout(callback, ms);
167+
changed: function(fn, ms) {
168+
var timer;
169+
return function(){
170+
clearTimeout(timer);
171+
timer = setTimeout(function(){
172+
fn();
173+
}, ms || self.interval);
190174
};
191-
}()
175+
}
176+
177+
};
192178

193-
}
179+
/**
180+
* Append visibility divs to <body> only after DOM laoded
181+
*/
182+
$(document).ready(function(){
183+
$.each(self.breakpoints, function(alias){
184+
self.breakpoints[alias].appendTo('body');
185+
});
186+
});
194187

195188
return self;
196189

js/bootstrap-toolkit.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/main.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,19 @@
2121
}
2222
}
2323

24-
// Executes once whole document has been loaded
25-
$(document).ready(function() {
24+
// Executes once whole document has been loaded
25+
$(document).ready(function() {
2626

2727
highlightBoxes();
2828

29-
console.log( 'Current breakpoint:', viewport.current() );
29+
console.log('Current breakpoint:', viewport.current());
3030

31-
});
32-
33-
34-
// Executes each time window size changes
35-
$(window).bind('resize', function() {
36-
viewport.changed(function(){
37-
38-
highlightBoxes();
39-
40-
console.log( 'Current breakpoint:', viewport.current() );
41-
42-
});
43-
});
31+
});
4432

33+
$(window).resize(
34+
viewport.changed(function(){
35+
console.log('Current breakpoint:', viewport.current());
36+
})
37+
);
4538

4639
})(jQuery, document, window, ResponsiveBootstrapToolkit);

0 commit comments

Comments
 (0)