Skip to content

Commit

Permalink
Add getModuleByDomNode
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Oct 5, 2016
1 parent 14dafaa commit 1af0318
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ interface ITerrificSpec {
export function createModule(name:string, spec: ITerrificSpec): TerrificModule;
export function startNode(node: Node): TerrificModule;
export function stopNode(node: Node): void;
export function getModuleByDom(node: HTMLElement): TerrificSpec;
export function bootstrap(): void;
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,23 @@ module.exports = {
return mod;
},

stopNode: function (domNode) {
/**
* Returns the Terrific Module instance for the given dom node
* @param {HTMLElement} domNode
*/
getModuleByDomNode: function (domNode) {
if (domNode.hasAttribute('data-t-id')) {
// link terrific component to dom node
var id = domNode.getAttribute('data-t-id');
return mainApplication.getModuleById(id);
}
return undefined;
},

stopNode: function (domNode) {
var module = this.getModuleByDomNode(domNode);
if (module) {
var modules = {};
modules[id] = mainApplication.getModuleById(id);
modules[domNode.getAttribute('data-t-id')] = module;
mainApplication.stop(modules);
mainApplication.unregisterModules(modules);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "terrific-singleton",
"version": "0.0.6",
"version": "0.0.7",
"description": "helper module to use terrific in react projects",
"repository": "https://github.com/jantimon/terrific-singleton",
"main": "index.js",
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,15 @@ describe('terrific-singleton', function () {
ts.Module.prototype.stop.restore();
});
});

describe('getModuleByDomNode', function () {
it('returns the module', function () {
var el = document.createElement('div');
var moduleConfig = { demo: function () {} };
ts.createModule('TestGetMod', moduleConfig);
el.setAttribute('data-t-name', 'TestGetMod');
ts.startNode(el);
assert.equal(ts.getModuleByDomNode(el).demo, moduleConfig.demo);
});
});
});

0 comments on commit 1af0318

Please sign in to comment.