Skip to content

Instantly share code, notes, and snippets.

@vangheem
Created September 22, 2016 15:20
Show Gist options
  • Save vangheem/ed0b3c0f62532bde0efd493adf568a47 to your computer and use it in GitHub Desktop.
Save vangheem/ed0b3c0f62532bde0efd493adf568a47 to your computer and use it in GitHub Desktop.
/* globals define */
define(function(){
'use strict';
var REGISTERED = {};
var register = function(name, config){
REGISTERED[name] = config;
};
var scan = function(baseDoc){
if(!baseDoc){
baseDoc = document;
}
for(var patternName in REGISTERED){
var pattern = REGISTERED[patternName];
baseDoc.querySelectorAll(pattern.selector).forEach(function(el){ // jshint ignore:line
if(el.patternInstance){
return;
}
el.patternInstance = pattern.init(el);
});
}
};
var get = function(name){
return REGISTERED[name];
};
return {
register: register,
scan: scan,
get: get
};
});
@thet
Copy link

thet commented Sep 22, 2016

Awesome implementation of the Patternslib scanner! If you don't need inside-out pattern initialization and options parsing, this is it.

This is the original implementation, by the way: https://github.com/Patternslib/Patterns/blob/master/src/core/registry.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment