Permalink
Browse files

Added ability to use '*' to run a function at every breakpoint

  • Loading branch information...
1 parent cab50b1 commit 33cd26da108a77dab353545fcc861c7780224110 @ten1seven committed Aug 20, 2012
Showing with 55 additions and 16 deletions.
  1. +15 −0 README.md
  2. +16 −6 index.html
  3. +18 −4 js/jRespond.js
  4. +6 −6 js/jRespond.min.js
View
@@ -69,6 +69,21 @@ jRes.addFunc({
});
```
+Use '*' to run a function at every breakpoint:
+
+``` JavaScript
+// register enter and exit functions for every breakpoint
+jRes.addFunc({
+ breakpoint: '*',
+ enter: function() {
+ myInitFunc();
+ },
+ exit: function() {
+ myUnInitFunc();
+ }
+});
+```
+
The breakpoint parameter is required but the enter and exit parameters are optional (of course, at least one is required for something to happen).
##Performance
View
@@ -53,21 +53,21 @@
jRes.addFunc({
breakpoint: 'desktop',
enter: function() {
- console.log('>>> run this for the desktop breakpoint <<<');
+ console.log('>>> run this for the DESKTOP breakpoint <<<');
outputStr.innerHTML = 'current breakpoint: desktop';
},
exit: function() {
- console.log('<<< destroy this when exiting the desktop breakpoint >>>');
+ console.log('<<< destroy this when exiting the DESKTOP breakpoint >>>');
}
});
jRes.addFunc({
breakpoint: ['laptop','tablet'],
enter: function() {
- console.log('>>> run this for the laptop/tablet breakpoint <<<');
+ console.log('>>> run this for the LAPTOP/TABLET breakpoint <<<');
},
exit: function() {
- console.log('<<< destroy this when exiting the laptop/tablet breakpoint >>>');
+ console.log('<<< destroy this when exiting the LAPTOP/TABLET breakpoint >>>');
}
});
@@ -88,11 +88,21 @@
jRes.addFunc({
breakpoint: 'handheld',
enter: function() {
- console.log('>>> run this for the handheld breakpoint <<<');
+ console.log('>>> run this for the HANDHELD breakpoint <<<');
outputStr.innerHTML = 'current breakpoint: handheld';
},
exit: function() {
- console.log('<<< destroy this when exiting the handheld breakpoint >>>');
+ console.log('<<< destroy this when exiting the HANDHELD breakpoint >>>');
+ }
+ });
+
+ jRes.addFunc({
+ breakpoint: '*',
+ enter: function() {
+ console.log('>>> run this when entering EVERY breakpoint <<<');
+ },
+ exit: function() {
+ console.log('<<< run this when exiting EVERY breakpoint >>>');
}
});
</script>
View
@@ -1,6 +1,6 @@
/*
* jRespond.js (a simple way to globally manage javascript on responsive websites)
- * version 0.7.2
+ * version 0.8.0
* (c) 2012 Jeremy Fields [jeremy.fields@viget.com]
* released under the MIT license
*/
@@ -78,15 +78,22 @@
// loops through all registered functions and determines if they should be fired
var cycleThrough = function() {
- var enterArray = [],
- exitArray = [];
+ var enterArray = [];
+ var exitArray = [];
for (var i = 0; i < mediaListeners.length; i++) {
var brkpt = mediaListeners[i]['breakpoint'];
var entr = mediaListeners[i]['enter'] || undefined;
var exit = mediaListeners[i]['exit'] || undefined;
- if (testForCurr(brkpt)) {
+ if (brkpt === '*') {
+ if (entr !== undefined) {
+ enterArray.push(entr);
+ }
+ if (exit !== undefined) {
+ exitArray.push(exit);
+ }
+ } else if (testForCurr(brkpt)) {
if (entr !== undefined && !mediaInit[i]) {
enterArray.push(entr);
}
@@ -128,10 +135,17 @@
// takes the breakpoint/s arguement from an object and tests it against the current state
var testForCurr = function(elm) {
+ // if there's an array of breakpoints
if (typeof elm === 'object') {
if (elm.join().indexOf(curr) >= 0) {
return true;
}
+
+ // if the string is '*' then run at every breakpoint
+ } else if (elm === '*') {
+ return true;
+
+ // or if it's a single breakpoint
} else if (typeof elm === 'string') {
if (curr === elm) {
return true;
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 33cd26d

Please sign in to comment.