Last active
September 21, 2015 15:05
-
-
Save ortense/36f725644de9e6bb9d81 to your computer and use it in GitHub Desktop.
Um POC divertido com class do es6
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
{ | |
"funcionarios" : [ | |
{"id":1, "nome":"Ortense", "cargo": "JS Developer", "id_empresa": 1}, | |
{"id":2, "nome":"Toschi", "cargo": "JS Developer", "id_empresa": 1}, | |
{"id":3, "nome":"Gafanhoto", "cargo": "JS Developer", "id_empresa": 1} | |
], | |
"empresas" : [ | |
{"id" : 1, "nome" : "Lúcida"}, | |
{"id" : 2, "nome" : "ACME"} | |
] | |
} |
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
"use strict"; | |
var db = require('./db.json'); | |
var Funcionario = require('./funcionario.class.js'); | |
class Empresa{ | |
constructor(id, nome){ | |
this.id = id; | |
this.nome = nome; | |
/* É aqui que a mágica acontece */ | |
//A promessa: | |
//temos 2 array e podemos manipula-los | |
this.funcionarios = []; | |
db.funcionarios.forEach( | |
f => { | |
if (f.id_empresa === this.id) { | |
this.funcionarios.push( | |
//A virada: | |
//quando os dados do db viram instâncias | |
new Funcionario( | |
f.id, | |
f.nome, | |
f.cargo, | |
this //O grande truque | |
) | |
); | |
} | |
} | |
); | |
} | |
} | |
module.exports = Empresa; |
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
"use strict"; | |
var db = require('./db.json'); | |
class Funcionario{ | |
constructor(id, nome, cargo, empresa){ | |
this.nome = nome; | |
this.cargo = cargo; | |
this.empresa = empresa; | |
} | |
} | |
module.exports = Funcionario; |
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
"use strict"; | |
var db = require('./db.json'); | |
var Empresa = require('./empresa.class.js'); | |
//busca a empresa no banco | |
let db_lucida = db.empresas.filter(empresa => empresa.id === 1)[0]; | |
//instancia da classe | |
let lucida = new Empresa( | |
db_lucida.id, | |
db_lucida.nome | |
); | |
//E aqui está a prova final | |
console.log(lucida.funcionarios); | |
console.log(lucida.funcionarios[0].empresa.nome); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment