Last active
February 3, 2019 10:34
-
-
Save vvscode/9439bcac8ee4a279fb7f to your computer and use it in GitHub Desktop.
Smartjs-Angulart-T1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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