forked from centresource/angularjs_rails_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllers.js
105 lines (84 loc) · 3.67 KB
/
controllers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* App Controllers */
/*
* Force the DOM to render bound data so you can access the DOM elements
*
* If the expected dom elements are not present when you try to execute
* forceRender on something, try a larger delay (for example, when rendering
* more complicated dom structures or a lot of data)
*/
function forceRender(arg1, arg2) {
var delay = (arg1 instanceof Function ? 5 : arg1);
var func = (arg1 instanceof Function ? arg1 : arg2);
/*
* http://docs.angularjs.org/#!/api/angular.service.$updateView reports
* that scope.$root.$eval() should force an immediate render; however,
* angular.scope().$root.$eval() wasn't immediate enough for new elements
* to be present when using jQuery on an expected DOM element
*/
angular.service('$updateView').delay = 0;
var updated_function = function() {
func();
angular.service('$updateView').delay = 25;
};
setTimeout(updated_function, delay);
}
function PhotoGalleryCtrl($route, $xhr) {
$xhr.defaults.headers.post['Content-Type'] = 'application/json'
$xhr.defaults.headers.put['Content-Type'] = 'application/json'
// assumes the presence of jQuery
var token = $("meta[name='csrf-token']").attr("content");
$xhr.defaults.headers.post['X-CSRF-Token'] = token;
$xhr.defaults.headers.put['X-CSRF-Token'] = token;
$xhr.defaults.headers.delete['X-CSRF-Token'] = token;
$route.when('/photographers',
{template: photographers_template, controller: PhotographersCtrl});
$route.when('/photographers/:photographer_id/galleries',
{template: galleries_template, controller: GalleriesCtrl});
$route.when('/photographers/:photographer_id/galleries/:gallery_id/photos',
{template: photos_template, controller: PhotosCtrl});
$route.otherwise({redirectTo: '/photographers'});
$route.onChange(function() {
this.params = $route.current.params;
});
}
function PhotographersCtrl(Photographers) {
this.photographers = Photographers.index();
}
function GalleriesCtrl(Galleries, Photographers) {
this.galleries = Galleries.index({photographer_id: this.params.photographer_id});
this.photographer = Photographers.get({photographer_id: this.params.photographer_id});
}
function PhotosCtrl(Photos, Galleries, Photographers, SelectedPhotos) {
var self = this;
self.clickPhoto = function(photo) {
var selected_photo = new SelectedPhotos({ selected_photo: {
photo_id: photo.id
}
});
selected_photo.$create(function() {
self.selected_photos.push(selected_photo);
});
}
self.deleteSelectedPhoto = function(selected_photo) {
angular.Array.remove(self.selected_photos, selected_photo);
selected_photo.$destroy({selected_photo_id: selected_photo.id});
}
self.saveSelectedPhoto = function(selected_photo) {
selected_photo.$update({ selected_photo_id: selected_photo.id });
$('input').blur();
}
self.gallery = Galleries.get({photographer_id: this.params.photographer_id, gallery_id: this.params.gallery_id});
self.photographer = Photographers.get({photographer_id: this.params.photographer_id});
Photos.index({photographer_id: this.params.photographer_id, gallery_id: this.params.gallery_id}, function(photos) {
self.photos = photos;
forceRender(function() {
$('#photos').cycle({fx: 'fade',
speed: 500,
timeout: 3000,
pause: 1,
next: '#next',
prev: '#prev'});
});
});
self.selected_photos = SelectedPhotos.index();
}