Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. app.controller('NewPostController', function($scope, $http) {
  2. $scope.selection = [];
  3. $http.get('/new_post').success(function(tags) {
  4. $scope.tags = tags;
  5. });
  6.  
  7. $scope.toggleSelection = function toggleSelection(tag) {
  8. var idx = $scope.selection.indexOf(tag);
  9.  
  10. // is currently selected
  11. if (idx > -1) {
  12. $scope.selection.splice(idx, 1);
  13. }
  14.  
  15. // is newly selected
  16. else {
  17. $scope.selection.push(tag);
  18. }
  19. };
  20.  
  21. $scope.addPost = function() {
  22. $scope.post.selection = $scope.selection;
  23. $http.post('new_post', $scope.post).success(function() {
  24. });
  25. }
  26.  
  27. class PostController extends BaseController {
  28. public function add() {
  29. if($post = Post::insertGetId(array('title' => Input::json('title'),
  30. 'body' => Input::json('body')))) {
  31. $tags = [];
  32. foreach(Input::json('selection') as $tag) {
  33. array_push($tags, $tag['id']);
  34. }
  35. $new_post = Post::find($post);
  36. $new_post->tags()->sync($tags);
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement