【发布时间】:2023-03-20 17:31:02
【问题描述】:
我在sails.js 的一个小项目中工作,我有两个模型:Empresa 和 Noticia,Empresa 模型有一个 Noticia 的集合,而 Noticia 模型有一个 empresa 关系:
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'
}
}
在前端我有一个 Angular 应用程序,它会将 Noticias 一个一个添加到 noticias 集合中,我该怎么做?
EmpresaNoticias angular.js 控制器
.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);
});
};
});
我知道在 javascript 中存在用于向数组添加值的函数 push,我想在sails.js 中做类似的事情,谢谢
【问题讨论】:
标签: javascript angularjs node.js sails.js waterline