public/rails_ext/rails_ext.js in rails-ext-0.2.6 vs public/rails_ext/rails_ext.js in rails-ext-0.2.7

- old
+ new

@@ -3,11 +3,11 @@ // $; $.template; $.toJSON; -js_commons = 'defined'; +railsExt = 'defined'; // // JavaScript Extension // Array.prototype.size = function () { @@ -30,10 +30,14 @@ inspect = function(data){ console.dir(data); }; +log = function(data){ + console.log(data); +}; + // // jQuery Extension // $.extend({ infoMessage: function(message){ @@ -98,40 +102,82 @@ }); // // jQuery Template // -$.template.regx.standard = /\#\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g; +// try{ +// $.template.regx.standard = /\#\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g; +// }catch(error){ +// console.info('jquery.template not installed!'); +// } // -// Behaviour Initializers +// Initializers // $.extend({ - initializers: [], + callOnceForEvery: function(name, query, initializer, dependencyLevel){ + if(!(name && query && initializer)) throw "Invalid arguments!"; + this._addInitializer({name: name, query: query, initializer: initializer}, this._processOnceForEveryInitializer, false, dependencyLevel) + }, - add_initializer: function(name, query, initializer){ - this.initializers.push({name: name + '_init', query: query, initializer: initializer}); + callOnceAfterUpdate: function(name, initializer, dependencyLevel){ + this._addInitializer({name: name, initializer: initializer}, this._processOnceAfterUpdateInitializer, true, dependencyLevel) }, - initialize: function() { - $($.initializers).each(function() { - var _this = this; - $(this.query).each(function(){ + callAfterUpdate: function(name, initializer, dependencyLevel){ + this._addInitializer({name: name, initializer: initializer}, this._processAfterUpdateInitializer, false, dependencyLevel) + }, + + _orderedInitializers: [[], [], [], [], [], []], + _addInitializer: function(definition, processor, callOnce, dependencyLevel){ + definition.name = '_' + definition.name + '_initialized'; + dependencyLevel = dependencyLevel || 0; + if(!(dependencyLevel >= 0 && dependencyLevel <= 5)) throw "Not supported Dependency Level Value: '" + dependencyLevel + "'!"; + this._orderedInitializers[dependencyLevel].push({definition: definition, processor: processor, callOnce: callOnce}); + }, + + _processInitializers: function(){ + for(var i = this._orderedInitializers.size() - 1; i >= 0; i--){ + var initializers = $(this._orderedInitializers[i]); + var toDelete = []; + initializers.each(function(i, v){ + // log(v.definition.name); + v.processor(v.definition); + if(v.callOnce) toDelete.push(i); + }); + $(toDelete.reverse()).each(function(){ + initializers.splice(this, this + 1); + }); + } + }, + + _processOnceForEveryInitializer: function(definition) { + // var count = 0 + $(definition.query).each(function(){ var e = $(this); - if(!e.data(_this.name)){ - e.data(_this.name, true); - _this.initializer.apply($(this)); + if(!e.hasClass(definition.name)){ // if(!e.data(definition.name)){ + // count = count + 1; + e.addClass(definition.name); // e.data(definition.name, true); + definition.initializer.apply(this); } }); - }); - } + // log(definition.name + ', ' + definition.query + ': ' + $(definition.query).size() + '/' + count) + }, + + _processOnceAfterUpdateInitializer: function(definition){ + definition.initializer(); + }, + + _processAfterUpdateInitializer: function(definition){ + definition.initializer(); + } }); $(function(){ - $.initialize(); + $._processInitializers(); }); $(document).ajaxSuccess(function(){ - $.initialize(); + $._processInitializers(); }); // // link_to // \ No newline at end of file