View change clears "unsaved" counter
[ajatus.git] / js / ajatus.renderer.js
blobb5b71b77e694c9d14786a4bd9bfd258ac7822bfe
1 (function($){
2     $.ajatus = $.ajatus || {};
4     $.ajatus.renderer_defaults = {
5         tick_limit: 20,
6         max_item_before_pool: 40        
7     };
8     
9     $.ajatus.renderer = function(settings) {
10         this.settings = $.extend($.ajatus.renderer_defaults, (settings || {}));
11         this.render_lock = false;           
12             this.items = [];
13             this.done_items = [];
14         };
15         $.extend($.ajatus.renderer.prototype, {
16             add_item: function(callback, args) {
17             var item = {
18                 callback: callback,
19                 args: args || []
20             };
21                 this.items.push(item);
22                 $.ajatus.events.lock_pool.increase();
23                 
24                 if (! this.render_lock) {
25                     this.start();
26                 }
27             },
28             
29             start: function() {
30                 this._create_lock();
31             },
32             
33             _on_tick: function() {
34                 var _self = this;
35                 if (this.items.length == 0)
36                 {
37                     $.ajatus.events.lock_pool.count = 0;
38                     return true;
39                 }
40                 this.items = $.grep(this.items, function(n, i){
41                     if (i < _self.settings.tick_limit)
42                     {                   
43                         var functionToCall = eval(n.callback);
44                         
45                     if (functionToCall.apply(functionToCall, n.args)) {
46                         _self.done_items.push(i);
47                         $.ajatus.events.lock_pool.decrease();          
48                     }
49                     } else {
50                         return true;
51                     }               
52                 });
53             },
54             
55             _create_lock: function() {
56                 var _self = this;
57             _self.render_lock = new $.ajatus.events.lock({
58                 disable_application: false,
59                 watch: {                    
60                     validate: function(){
61                         _self._on_tick();
62                         return $.ajatus.events.lock_pool.count == 0;
63                     },
64                     interval: 400,
65                     safety_runs: 2
66                 },
67                 on_release: function(){_self._lock_finished();}
68             });
69             },
70             
71             _lock_finished: function() {
72             }
73         });
75 })(jQuery);