0
I am developing an application that uses a lot of Jquery, since there are several very specific functions.
But for reasons of customization and optimization, I managed with the mix of some researches to create "my own lib" in a very simplistic way, nothing great, just in order to perform functions in the front end that style the site from a pattern of its own. The link to a part of it is here on github.
And created the function .newElement()
, that in the case below works perfectly.
var div = M('div');
div.css({
width: '100px',
padding: '10px',
background: "#333"
})
var a = div.newElement('a');
a.css({
color: 'white'
})
a.html('Exemplo')
a.attrs({
href: "https://jsfiddle.net/",
target: "_blank"
})
<script src="https://rawgit.com/samirbraga/libM/master/mlib.js"></script>
<div></div>
But when I try to raise more than one child to the same element, I have a problem:
var div = M('div');
div.css({
width: '100px',
padding: '10px',
background: "#333"
})
var a = div.newElement('a');
a.css({
color: 'white'
})
a.html('Exemplo')
a.attrs({
href: "https://jsfiddle.net/",
target: "_blank"
})
var p = div.newElement('p');
p.html('Texto que não deveria ser adicionado ao link')
<script src="https://rawgit.com/samirbraga/libM/master/mlib.js"></script>
<div></div>
The new child, which by code should be added to the same element, is added to the child. In this case div > a > p
, what should be div > a, p
.
The strange thing is that if I don’t pass the element I want to add to a variable div = M('div')
and instead use M('div').newElement('p')
, works.
I don’t know what’s causing it.
The code of the function newElement()
:
newElement: function(element){
var _newElement;
each(self.myArg, function(el, i) {
_newElement = document.createElement(element)
el.appendChild(_newElement);
})
return M(_newElement)
},
From now on, thank you.
What is
self.myArg
?– Sergio
@Sergio, it’s the general function argument.
M('.div')
, in the case.div
would be returned in theself.myArg
.– Samir Braga
While I saw the Github link, I went to see there
– Sergio
It’s late here and I won’t be able to analyze much. But what is the idea of
.myIndex
?– Sergio
@Sergio, It would be a second argument of the function, selects the element by the index. Ex
M('div', 2)
. Use as another option forM('div').eq(2)
.– Samir Braga
Okay, and why do you have
var myArg;
 var self;
global? There is a great risk of being overwritten by methods on different elements...– Sergio
@Sergio, that’s true. In this case, I don’t even use the
myArg
, but I use theself
. How do you think you should go?– Samir Braga
Creating a new library is complex. If you only have a few methods, fine. Otherwise it’s better to use something that already exists. And there’s a lot more out there than jQuery :P But here’s an idea of the approach you could take: https://jsfiddle.net/6883xzsh/
– Sergio
@Sergio, and I’ve had contact with some :P, the idea wasn’t even to create a whole lib, it was just the app’s own methods that I’m creating, like selects Places, etc. The real focus, it was just substituting elements, so the method I would use the most was this
newElement
. I’ll take a look at your fiddle.– Samir Braga
@Sergio, I took a look. It’s, in your example, with class and the constructors are working fine. But I still haven’t been able to apply in mine, I think the problem is in the loop. Anyway, thanks for the attention. :)
– Samir Braga