Skip to content

Commit

Permalink
injections
Browse files Browse the repository at this point in the history
  • Loading branch information
eavichay committed May 17, 2017
1 parent d0340ca commit 5109d0e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 32 deletions.
48 changes: 34 additions & 14 deletions Slim.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ var Slim = function (_CustomElement2) {
Slim.__templateDict[_tag] = clazzOrTemplate;
}
Slim.__prototypeDict[_tag] = clazz;
// window.customElements.define(tag, clazz);
document.registerElement(_tag, clazz);
if (Slim.__prototypeDict['slim-repeat'] === undefined) {
Slim.__initRepeater();
}
setTimeout(function () {
document.registerElement(_tag, clazz);
}, 0);
}

//noinspection JSUnusedGlobalSymbols
Expand Down Expand Up @@ -144,15 +148,18 @@ var Slim = function (_CustomElement2) {
if (target.remove) {
target.remove();
}
if (!target.remove && target.parentNode) {
if (target.parentNode) {
target.parentNode.removeChild(target);
if (target._boundChildren) {
target._boundChildren.forEach(function (child) {
if (child.__ieClone) {
Slim.removeChild(child.__ieClone);
}
});
}
}
if (target.__ieClone) {
Slim.removeChild(target.__ieClone);
}
if (target._boundChildren) {
target._boundChildren.forEach(function (child) {
if (child.__ieClone) {
Slim.removeChild(child.__ieClone);
}
});
}
}

Expand Down Expand Up @@ -260,6 +267,22 @@ var Slim = function (_CustomElement2) {
}
return { source: desc, prop: prop, obj: obj };
}
}, {
key: '__inject',
value: function __inject(descriptor) {
try {
descriptor.target[Slim.__dashToCamel(descriptor.attribute)] = Slim.__injections[descriptor.factory](descriptor.target);
} catch (err) {
console.error('Could not inject ' + descriptor.attribute + ' into ' + descriptor.target);
console.info('Descriptor ', descriptor);
throw err;
}
}
}, {
key: 'inject',
value: function inject(name, injector) {
Slim.__injections[name] = injector;
}

/**
*
Expand All @@ -270,9 +293,6 @@ var Slim = function (_CustomElement2) {
}, {
key: '__createRepeater',
value: function __createRepeater(descriptor) {
if (Slim.__prototypeDict['slim-repeat'] === undefined) {
Slim.__initRepeater();
}
var repeater = void 0;
repeater = document.createElement('slim-repeat');
repeater.sourceNode = descriptor.target;
Expand Down Expand Up @@ -1130,7 +1150,6 @@ var Slim = function (_CustomElement2) {
}, {
key: 'extract',
value: function extract(target, expression) {
var rxInject = Slim.rxInject.exec(expression);
var rxProp = Slim.rxProp.exec(expression);
var rxMethod = Slim.rxMethod.exec(expression);

Expand Down Expand Up @@ -1217,6 +1236,7 @@ Slim.__customAttributeProcessors = {};
Slim.__prototypeDict = {};
Slim.__uqIndex = 0;
Slim.__templateDict = {};
Slim.__injections = {};
Slim.__plugins = {
'create': [],
'beforeRender': [],
Expand Down
2 changes: 1 addition & 1 deletion Slim.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions example/tests/test-inject.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

Slim.tag('inject-demo', '<h1 bind>Hello [[modelName.e]]</h1>', class extends Slim {
onBeforeCreated() {
console.log('onBeforeCreated', this.model);
console.log('onBeforeCreated', this.modelName);
}

onCreated() {
console.log('onCreated', this.model);
console.log('onCreated', this.modelName);
}

onBeforeRender() {
console.log('onBeforeRender', this.model);
console.log('onBeforeRender', this.modelName);
}
});

Expand Down
48 changes: 34 additions & 14 deletions src/Slim.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ class Slim extends HTMLElement {
Slim.__templateDict[tag] = clazzOrTemplate;
}
Slim.__prototypeDict[tag] = clazz;
// window.customElements.define(tag, clazz);
document.registerElement(tag, clazz);
if (Slim.__prototypeDict['slim-repeat'] === undefined) {
Slim.__initRepeater();
}
setTimeout( () => {
document.registerElement(tag, clazz);
}, 0);
}

//noinspection JSUnusedGlobalSymbols
Expand Down Expand Up @@ -109,15 +113,18 @@ class Slim extends HTMLElement {
if (target.remove) {
target.remove();
}
if (!target.remove && target.parentNode) {
if (target.parentNode) {
target.parentNode.removeChild(target);
if (target._boundChildren) {
target._boundChildren.forEach( child => {
if (child.__ieClone) {
Slim.removeChild(child.__ieClone);
}
});
}
}
if (target.__ieClone) {
Slim.removeChild(target.__ieClone);
}
if (target._boundChildren) {
target._boundChildren.forEach( child => {
if (child.__ieClone) {
Slim.removeChild(child.__ieClone);
}
});
}
}

Expand Down Expand Up @@ -175,15 +182,28 @@ class Slim extends HTMLElement {
return {source: desc, prop:prop, obj:obj};
}

static __inject(descriptor) {
try {
descriptor.target[ Slim.__dashToCamel(descriptor.attribute) ] = Slim.__injections[ descriptor.factory ](descriptor.target);
}
catch (err) {
console.error('Could not inject ' + descriptor.attribute + ' into ' + descriptor.target);
console.info('Descriptor ', descriptor);
throw err;
}

}

static inject(name, injector) {
Slim.__injections[name] = injector;
}

/**
*
* @param descriptor
* @private
*/
static __createRepeater(descriptor) {
if (Slim.__prototypeDict['slim-repeat'] === undefined) {
Slim.__initRepeater();
}
let repeater;
repeater = document.createElement('slim-repeat');
repeater.sourceNode = descriptor.target;
Expand Down Expand Up @@ -482,7 +502,6 @@ class Slim extends HTMLElement {
* @returns {*}
*/
static extract(target, expression) {
const rxInject = Slim.rxInject.exec(expression);
const rxProp = Slim.rxProp.exec(expression);
const rxMethod = Slim.rxMethod.exec(expression);

Expand Down Expand Up @@ -933,6 +952,7 @@ Slim.__customAttributeProcessors = {};
Slim.__prototypeDict = {};
Slim.__uqIndex = 0;
Slim.__templateDict = {};
Slim.__injections = {};
Slim.__plugins = {
'create': [],
'beforeRender': [],
Expand Down

0 comments on commit 5109d0e

Please sign in to comment.