Skip to content

Commit

Permalink
Add helper for refresh+query
Browse files Browse the repository at this point in the history
Resolves #72
  • Loading branch information
Sam Richard committed Apr 28, 2016
1 parent 762a94e commit c169a29
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ When **eq.js** has determined which state your element is in, it will add an `da

**eq.js** also adds `window.eqjs` to allow you to utilize **eq.js** in your own function calls. It will handle your `DOMContentLoaded` and `load` events as well as all `resize` events, inspecting your DOM to determine what nodes need to be queried each time.

If you dynamically add nodes that you would like to query, you need to trigger **eq.js** yourself. This is easy though! Just load up your nodes into an array or a NodeList and pass that to `eqjs.query(nodes)`, and **eq.js** will work its magic. `eqjs.query()` also takes a callback as an optional second argument that will be fired once all of the nodes have been processed. It will be passed an array of nodes that were worked on.
If you dynamically add nodes that you would like to query, you need to trigger **eq.js** yourself. This is easy though! Just load up your nodes into an array or a NodeList and pass that to `eqjs.query(nodes[, cb])`, and **eq.js** will work its magic. `eqjs.query()` also takes a callback as an optional second argument that will be fired once all of the nodes have been processed. It will be passed an array of nodes that were worked on. You can also call `eqjs.all([cb])` to run **eq.js** against all nodes in the DOM (with an optional `cb` callback).

Each node that gets queried will also fire an `eqResize` event once **eq.js** has worked its magic. This'll allow you to code reactively to what happens! The current value of `data-eq-state` will be available in `event.details`;

Expand Down
27 changes: 21 additions & 6 deletions build/eq.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,24 @@
return points;
};

/**
* Query All Nodes
* Runs refreshNodes and Query
**/
EQjs.prototype.all = function (cb) {
var proto = Object.getPrototypeOf(eqjs);
var hasCB = cb ? true : false;

proto.refreshNodes();

if (!hasCB) {
window.requestAnimationFrame(proto.query);
}
else {
proto.query(undefined, cb);
}
}

/*
* We only ever want there to be
* one instance of EQjs in an app
Expand All @@ -280,16 +298,14 @@
* Fires on document load; for HTML based EQs
*/
addEvent(window, 'DOMContentLoaded', function () {
eqjs.refreshNodes();
eqjs.query(undefined, true);
eqjs.all(false);
});

/*
* Window Loaded
*/
addEvent(window, 'load', function () {
eqjs.refreshNodes();
eqjs.query(undefined, true);
eqjs.all(true);
});

/*
Expand All @@ -298,8 +314,7 @@
* Loop over each `eq-pts` element and pass to eqState
*/
addEvent(window, 'resize', function () {
eqjs.refreshNodes();
window.requestAnimationFrame(eqjs.query);
eqjs.all(true);
});

// Expose 'eqjs'
Expand Down
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = function(config) {

// list of files / patterns to load in the browser
files: [
'bower_components/polyfill-service/polyfills/requestAnimationFrame/polyfill.js',
'bower_components/polyfill-service/polyfills/CustomEvent/polyfill.js',
'build/**/*.js',
'tests/**/*.js'
Expand Down
11 changes: 11 additions & 0 deletions tests/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,15 @@ describe('Set the `data-eq-pts` attribute via JavaScript', function () {
expect(eventSpy).toHaveBeenCalled();
});
});

//////////////////////////////
// All Query
//////////////////////////////
it('should query properly with `all` query', function () {
body.style.width = (sizes[2]) + 'px';
eqjs.all(function () {
result = elem.getAttribute('data-eq-state');
expect(result).toBe('small medium large');
});
});
});

0 comments on commit c169a29

Please sign in to comment.