Callback em um objecto
Boas, é possivel usar uma funçao de callback que está dentro de um objecto ?
Código:
var Resources = {
init: function() {
this.CSSfiles = [];
this.Add = Resources.Add;
this.Load = Resources.Load;
this.cssState = 0;
},
Add: function(json) {
this.CSSfiles.push(json);
},
Load: function(callback) {
var head = document.getElementsByTagName("head")[0]
this.CSSfiles.forEach(function(file) {
var ref = document.createElement('link');
ref.setAttribute('rel', 'stylesheet');
ref.setAttribute('type', 'text/css');
ref.setAttribute('async', file['async']);
ref.setAttribute('href', file['file']);
head.appendChild(ref);
});
callback.call();
}
}
var obj = {
Create: function(files, html, id) {
this.files = files;
this.html = html;
this.id = id;
this.Load = obj.Load;
this.insertHtml = obj.insertHtml;
},
Load: function() {
var Resc = new Resources.init();
this.files.forEach(function(file) {
Resc.Add(file, 'css');
});
Resc.Load(this.insertHtml);
},
insertHtml: function(){
// just to test
console.log('id ='+this.id);
console.log('html ='+this.html);
}
}
var cs = new obj.Create([{"file":"http:\/\/dev.localhost\/rsc.php?file=css\/core\/block_left.css","async":false},{"file":"http:\/\/dev.localhost\/rsc.php?file=css\/core\/blockleft.css","async":false}],'<h1>test</h1>', 'content');
cs.Load();
A callback é chamada na funçao Load do Resources e está dentro do objecto "obj".
http://jsfiddle.net/3j7c8hjh/
Discussão (0)
Carregando comentários...