我几个小时以来一直在努力,但我坚持下去。我没有得到所需的输出。我能够在集合中插入/更新数组,但设计并不是我想要的。
期望的输出:
"app_mode_list" : [
{
"name" : "Office",
"_id" : "OFFICE"
},
{
"name" : "Cash and Carry",
"_id" : "CASH_AND_CARRY"
}
]
输出,我在mongo厨师(收藏中)获得的内容:
"app_mode_list" : {
"1" : {
"name" : "Office",
"_id" : "OFFICE"
},
"0" : {
"name" : "Cash and Carry",
"_id" : "CASH_AND_CARRY"
}
}
架构设计:
var PortalSchema = new Schema({
name:String,
app_mode_list :[tokenRefSchema]
}, {
collection:'portal',
timestamps:{createdAt:'created_at', updatedAt:'updated_at'}
}
);
var TokenRefSchema = Schema({
_id:String,
name:String
},
{_id: false}
);
控制器: 我正在使用数组名称" app_mode_list"推送其中的所有记录。
app_mode_list.push({"_id":$scope.portalSettings.app_mode_list._id,
"name":$scope.portalSettings.app_mode_list.name});
然后,在使用 save 按钮之前,我将此数组投射到我的示波器。
$scope.portalSettings.app_mode_list = app_mode_list;
完整控制器代码:
angular.module('rugCoPro')
.controller('PortalSettingsController', ['$scope', 'Session', '$location', 'ObjectUtils', '$http', '$state', '$stateParams', 'JsonRest','$mdDialog','$mdToast', function($scope, session, $location, ObjectUtils, $http, $state, $stateParams, JsonRest,$mdDialog,$mdToast){
if(!session.isAuthenticated()){
session.logout();
}
$http.get("api/models/payroll_frequency").success(function(response, status, headers){
$scope.payrollFrequencies = response;
});
$http.get("api/models/app_mode").success(function(response, status, headers){
$scope.appModes = response;
});
$scope.portalSettings = $stateParams.portal;
console.log("portalSettings: --->> "+ JSON.stringify($scope.portalSettings, null, " "));
if(ObjectUtils.isObject($scope.portalSettings)){
$scope.title = "Editing Settings";
$scope.app_mode_list = $scope.portalSettings.app_mode_list;
console.log("1 $scope.app_mode_list : ", JSON.stringify($scope.app_mode_list, null, " "));
console.log(" 1 $scope.portalSettings.app_mode_list : ", JSON.stringify( $scope.portalSettings.app_mode_list, null, " "));
}
$scope.cancel = function(){
$state.go($stateParams.backState);
};
$scope.save = function(){
if(ObjectUtils.isObject($scope.portalSettings)){
var req;
$scope.portalSettings.app_mode_list = ($scope.app_mode_list);
console.log(" $scope.app_mode_list : ", JSON.stringify($scope.app_mode_list, null, " "));
console.log(" $scope.portalSettings.app_mode_list : ", JSON.stringify( $scope.portalSettings.app_mode_list, null, " "));
var sendableEntry = JsonRest.sanitize($scope.portalSettings, true);
req = $http.put("api/portal/" + $scope.portalSettings._id, sendableEntry);
req.success(function(response, status, headers){
var user = session.getUser();
if(ObjectUtils.isObject(response) && ObjectUtils.isObject(user)){
user.portal = response;
session.setUser(user);
};
$state.go($stateParams.backState);
});
}
};
$scope.app_mode_list = [];
// a list born with awesome features.
// yeah.. welcome aboard a new entry
$scope.addToList = function(){
// if(ObjectUtils.isObject($scope.portalSettings.app_mode_list)){
if($scope.portalSettings.app_mode_list === undefined){
var msg = "Please select a 'App Mode'!!"
showSimpleToast(msg);
}
else{
if(findIndexItem($scope.portalSettings.app_mode_list) == '-1'){
$scope.app_mode_list.push(
{
"_id" : $scope.portalSettings.app_mode_list._id,
"name" : $scope.portalSettings.app_mode_list.name
}
);
console.log("$scope.app_mode_list : " + JSON.stringify($scope.app_mode_list, null, " "));
}
else{
var msg = "'" + $scope.portalSettings.app_mode_list.name + "' is already in the list."
showSimpleToast(msg);
}
}
};
// sarifice what make you unforgettable. bye bye you old entry!!
$scope.deleteFromList = function(item, event){
var confirmDialog = $mdDialog.confirm()
.title('Are you sure?')
.textContent('Do you want to delete <' + item.name + ">?")
.ariaLabel('Confirm Delete Dialog')
.ok('Yes')
.cancel('No')
.targetEvent(event);
$mdDialog.show( confirmDialog )
.then(function(){
$scope.app_mode_list.splice(findIndexItem(item.name),1);
});
};
// okay scout! lets find your place. :)
var findIndexItem= function(argu){
// no need to check the hold
var i = null;
for (i = 0; $scope.app_mode_list.length > i; i += 1) {
if ($scope.app_mode_list[i].name == argu.name) {
return i;
}
}
return -1;
}
//i am hungrey, where is my toast.
var showSimpleToast = function(msg) {
$mdToast.show(
$mdToast.simple()
.textContent(msg)
.hideDelay(3000)
.position('bottom right')
);
};
}]);