4 ms·
There is no _major_ problem, but the syntax can be terrible. May be I'm wrong but, the main problem for me was to access the current instance of a class. 1st l
by yogsototh 16y ago
There is no _major_ problem, but the syntax can be terrible.
May be I'm wrong but, the main problem for me was to access the current instance of a class.
1st level : use this
2nd level : var self=this; then use self;
3rd level : var sefl=this; and pass self as parameter.
Typically, when a new view appear, I load dynamically the resources nee: other javascript files and an HTML template file. I use jquery to handle AJAX.
Here is some code:
ConsumptionView.prototype.show = function(){
var self=this;
var files=[];
var tests=[];
files.push('/static/js/date.js');
tests.push('Date.prototype.setISO8601');
mainApplication.run_after_dependencies( files, tests,
function() {
$('#content').load("/static/html/user_consumption.html",
function(){ self.htmlLoaded(self);});
});
}
the `run_after_dependencies` function will mostly do some getScript only if needed. The last function will do (mostly):
$.getScript('/static/js/data.js', function() {
$('#content').load("/static/html/user_consumption.html",
function() { self.htmlLoaded(self);}
);}
);
But in the end, I _have_ to pass 'self' in parameter to 'htmlLoaded' function and I cannot use the 'var self=this;' at the top of the definition of the function html_loaded.
- demallien 16y agoOK, you don't like what is happening at level 3, is that right? Well, I see only two possibilities - either your htmlLoaded function has different semantics for its this and the parameter self, or it doesn't need the parameter. In the first case, repetition is necessary, because you might want to do this: view.htmlLoaded(other_view); in the second case the function should be more like: ConsumptionView.prototype.htmlLoaded = function() { this.doStuff(); }