Implement lazy js load wraper

This commit is contained in:
YNH Webdev
2013-09-28 00:45:27 +02:00
parent aa9f4c4ed8
commit cb1a4825d2
3 changed files with 53 additions and 36 deletions

View File

@@ -1,5 +1,5 @@
// Generated by CoffeeScript 1.6.1
var Mini, WSF_AUTOCOMPLETE_CONTROL, WSF_BUTTON_CONTROL, WSF_CHECKBOX_CONTROL, WSF_CHECKBOX_LIST_CONTROL, WSF_CONTROL, WSF_FORM_ELEMENT_CONTROL, WSF_GRID_CONTROL, WSF_HTML_CONTROL, WSF_INPUT_CONTROL, WSF_MAX_VALIDATOR, WSF_MIN_VALIDATOR, WSF_PAGE_CONTROL, WSF_PAGINATION_CONTROL, WSF_PROGRESS_CONTROL, WSF_REGEXP_VALIDATOR, WSF_REPEATER_CONTROL, WSF_TEXTAREA_CONTROL, WSF_VALIDATOR, build_control, cache, controls, show_alert, start_modal, template, tmpl,
var Mini, WSF_AUTOCOMPLETE_CONTROL, WSF_BUTTON_CONTROL, WSF_CHECKBOX_CONTROL, WSF_CHECKBOX_LIST_CONTROL, WSF_CONTROL, WSF_FORM_ELEMENT_CONTROL, WSF_GRID_CONTROL, WSF_HTML_CONTROL, WSF_INPUT_CONTROL, WSF_MAX_VALIDATOR, WSF_MIN_VALIDATOR, WSF_PAGE_CONTROL, WSF_PAGINATION_CONTROL, WSF_PROGRESS_CONTROL, WSF_REGEXP_VALIDATOR, WSF_REPEATER_CONTROL, WSF_TEXTAREA_CONTROL, WSF_VALIDATOR, build_control, cache, controls, lazy_load, show_alert, start_modal, template, tmpl,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
@@ -49,6 +49,31 @@ Mini = {
}
};
lazy_load = function(requirements, fn, that) {
if (that == null) {
that = window;
}
return function() {
var args, counter, done, r, self, _i, _len;
if (typeof args === "undefined" || args === null) {
args = [];
}
counter = requirements.length + 1;
self = this;
done = function() {
counter = counter - 1;
if (counter === 0) {
fn.apply(that, arguments);
}
};
for (_i = 0, _len = requirements.length; _i < _len; _i++) {
r = requirements[_i];
$.cachedScript(r).done(done);
}
return done();
};
};
build_control = function(control_name, state, control) {
var $el, type, typeclass;
$el = control.$el.find('[data-name=' + control_name + ']');
@@ -151,27 +176,10 @@ WSF_CONTROL = (function() {
this.load_subcontrols();
this.isolation = "" + this.$el.data('isolation') === "1";
this.$el.data('control', this);
this.initialize = lazy_load(this.requirements, this.attach_events, this);
return;
}
WSF_CONTROL.prototype.initialize = function() {
var counter, done, r, self, _i, _len, _ref;
counter = this.requirements.length + 1;
self = this;
done = function() {
counter = counter - 1;
if (counter === 0) {
self.attach_events();
}
};
_ref = this.requirements;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
r = _ref[_i];
$.cachedScript(r).done(done);
}
return done();
};
WSF_CONTROL.prototype.load_subcontrols = function() {
var control_name, state;
if (this.fullstate.controls != null) {
@@ -339,6 +347,7 @@ WSF_PAGE_CONTROL = (function(_super) {
this.url = this.state['url'];
this.url_params = jQuery.unparam(this.state['url_params']);
this.$el.data('control', this);
this.initialize = lazy_load(this.requirements, this.attach_events, this);
this.load_subcontrols();
}
@@ -802,7 +811,7 @@ show_alert = function(action) {
return alert(action.message);
};
start_modal = function(action) {
start_modal = lazy_load(['assets/bootstrap.min.js'], function(action) {
var modal;
modal = $("<div class=\"modal fade\">\n<div class=\"modal-dialog\">\n <div class=\"modal-content\">\n <div class=\"modal-header\">\n <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-hidden=\"true\">&times;</button>\n <h4 class=\"modal-title\">" + action.title + "</h4>\n </div>\n <div class=\"modal-body\">\n \n </div>\n </div>\n</div>\n</div>");
modal.appendTo('body');
@@ -817,4 +826,4 @@ start_modal = function(action) {
}).done(function(data) {
return modal.find('.modal-body').append(data);
});
};
});