Skip to content

Instantly share code, notes, and snippets.

@Dubiy
Last active February 16, 2016 12:26
Show Gist options
  • Save Dubiy/70dc141a6af2faeaceb1 to your computer and use it in GitHub Desktop.
Save Dubiy/70dc141a6af2faeaceb1 to your computer and use it in GitHub Desktop.
example of editor controller for symfony
angular.module('DemoEkBlog')
.controller('IssueEditorCtrl', ['$rootScope', '$scope', '$http', 'ModalService', 'Upload', '$timeout', IssueEditorCtrl]);
function IssueEditorCtrl($rootScope, $scope, $http, ModalService, Upload, $timeout) {
$rootScope.issue = {};
$scope.categoryId = '';
$scope.init = (data) => {
$scope.colors = data.colors;
$scope.page = data.page;
$scope.categories = data.categories;
$rootScope.issue = data.issue;
if ($rootScope.issue.id == null) {
$rootScope.issue.id = 0;
}
$scope.recalcColor();
};
$scope.saveIssue = (form) => {
let errors = '';
if (!form.$valid) {
errors += 'Please, complete the form\n';
}
if ($rootScope.issue.categories.length == 0) {
errors += 'Please, choose at least one category\n';
}
if (errors) {
alert(errors);
return;
}
$rootScope.spinner = true;
let categories = [];
$rootScope.issue.categories.forEach((el) => {
categories.push(el.id);
});
$http.post(Routing.generate('issue_save', {id: $rootScope.issue.id}), {
issue: {
title: $rootScope.issue.title,
color: $rootScope.issue.color,
categories: categories
}
})
.then(function (resolve) {
$rootScope.issue.id = resolve.data.issue.id;
document.location.href = Routing.generate('issue_index', {page: $scope.page});
console.log('ok, saved');
//$rootScope.spinner = false;
}, function(reject) {
let res = reject.data.error.message + "\n";
reject.data.error.info.children.forEach((el) => {
res += ' ' + el.field + ': ';
if (typeof el.errors != 'undefined') {
el.errors.forEach((error) => {
res += error.message + ' ';
});
} else {
el.children.forEach((subchild) => {
subchild.children.forEach((subsubchild) => {
subsubchild.errors.forEach((error) => {
res += error.message + ' ';
});
});
});
}
res += '\n';
});
alert(res);
console.log(reject);
$rootScope.spinner = false;
});
};
$scope.removeCategory = (category) => {
if (confirm('Remove "' + category.title + '" category?')) {
let index = $rootScope.issue.categories.indexOf(category);
$rootScope.issue.categories.splice(index, 1);
}
};
$scope.hasCategory = (category) => {
try {
$rootScope.issue.categories.forEach((el) => {
if (el.id == category.id) {
throw '';
}
});
return false;
} catch (e) {
return true;
}
};
$scope.addCategory = (categoryId) => {
$scope.categories.forEach((el) => {
if (el.id == categoryId) {
$rootScope.issue.categories.push(el);
}
});
$scope.categoryId = '';
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment