Skip to content

Instantly share code, notes, and snippets.

@vvscode
Last active February 3, 2019 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vvscode/9439bcac8ee4a279fb7f to your computer and use it in GitHub Desktop.
Save vvscode/9439bcac8ee4a279fb7f to your computer and use it in GitHub Desktop.
Smartjs-Angulart-T1
(function(window) {
var directives = {};
var pairRegExp = /^(.+?)=(.+?)$/;
var myNg = {
directive: function(name, fn) {
directives[name] = {
name: name,
fn: fn
};
return this;
},
process: function(el) {
[].slice.call(el.attributes, 0).forEach(function(pair) {
var attrName = pair.name;
var attrValue = pair.value;
var directive = directives[attrName];
if(!!directive) {
directive.fn(el, attrValue);
}
});
[].slice.call(el.children, 0).forEach(this.process.bind(this));
return this;
}
};
return window.myNg = myNg;
})(window);
(function() {
window.foo = {};
window.onClick = function() {
alert('onClick from mng-click \n\n' + this.innerHTML);
};
myNg
.directive('mng-p', function pDirective(el, param) {
param = param || "";
el.classList.add('mng-p');
if(param) {
el.classList.add('mng-p-' + param);
}
})
.directive('mng-model', function(el, param) {
var setValue = function() {
try{
eval('' + param +' = "' + el.value + '"');
} catch (e) {
console.warn('mng-model:setValue: ', e);
}
};
el.addEventListener('change', setValue);
setValue();
})
.directive('mng-click', function(el, param) {
el.addEventListener('click', function() {
if(typeof window[param] === 'function') {
window[param].apply(this, arguments);
}
});
})
.process(document.querySelector('body'));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment