casopisi 2010
[angular-mojolicious.git] / public / app / casopisi2010 / upitnik.js
1
2 if (typeof (console) === 'undefined') console = { debug: function() {} }; // mock console.debug
3
4 var database = 'casopisi2010';
5
6 function Upitnik($resource,$xhr){
7         this.master = {
8                 casopisi: [{naslov:''},{naslov:''},{naslov:''}],
9                 eizvori: [{naziv:''}]
10         };
11         this.Upitnik = $resource( '/data/:database/Upitnik/:id', { id:'', database: database } );
12         this.reset();
13         this.$watch('$location.hashPath', this.hash_change);
14         this.$xhr = $xhr;
15         console.debug( 'database', database, this.upitnik );
16 }
17 Upitnik.$inject=['$resource','$xhr'];
18
19 Upitnik.prototype = {
20         hash_change: function() {
21                 var id = this.$location.hashPath;
22 console.debug( 'hash_change', id, this.upitnik.$id );
23                 if ( id != this.upitnik.$id ) {
24                         if (id) {
25                                 var self = this;
26                                 this.Upitnik.get({ id: id }, function(upitnik) {
27 console.debug('upitnik', id, upitnik);
28                                         self.last_saved = angular.copy(upitnik);
29                                         self.upitnik = upitnik; // needed for load_symposium below
30                                 });
31                         }
32                         else this.reset();
33                 }
34         },
35         reset: function() {
36                 console.debug( this.Upitnik );
37                 this.upitnik = new this.Upitnik( this.master );
38 console.debug( 'reset', this.upitnik );
39                 this.last_saved = this.upitnik;
40         },
41         save: function(){
42                 var self = this;
43                 this.upitnik.$save(function(upitnik){
44                         self.$location.hashPath = upitnik.$id;
45                         self.last_saved = angular.copy(upitnik);
46                 });
47         }
48 };
49