I'm working in a little project in sails.js, i have two models: Empresa and Noticia, the Empresa Model have a collection of Noticia and the Noticia model have a empresa relation:
Empresa.js
module.exports = {
attributes: {
nombre: {
type: 'string'
},
activa:{
type: 'boolean',
defaultsTo: false
},
usuarios:{
collection: 'User',
via: 'empresa'
},
noticias:{
collection: 'Noticia',
via: 'empresa'
}
}
};
Noticia.js
module.exports = {
attributes: {
titulo:{
type: 'string',
required: true
},
texto:{
type: 'string',
required: true
},
publicada:{
type: 'boolean',
defaultsTo: false
},
empresa:{
model: 'Empresa',
via: 'noticias'
}
}
in the front end i have a angular app whose is gonna add Noticias to the noticias collection one by one, how can i do it?
EmpresaNoticias angular.js controller
.controller('EmpresaUsuarioController', function ($scope, $routeParams, $location, Noticia, Empresa) {
$scope.addNoticia = function(){
$scope.newNoticia.empresa = $routeParams.EmpresaId;
Noticia.save($scope.newNoticia, function(noticia){
Empresa.update({id: $routeParams.EmpresaId}, {noticias: noticia.id}, function(){
$location.path('/noticia/'+ noticia.id);
})
}, function(error){
console.log(error);
});
};
});
i know that in javascript exists the function push to add values to an array, and i want to do something like that in sails.js, Thanks