(duplicate of http://stackoverflow.com/q/23142089/3546614)
(duplicate of https://stackoverflow.com/q/23142089/3546614)
Update 1
I've just updated my code(and truncated unimportant stuff) taking @jgillich's advices into account. For the moment I feel better reading the JSON file when the object is generated which is why I outsourced the operation into separate function.$(function(){
function Model() {
this.loadJson();
};
Model.prototype = {
deferred: $.Deferred(),
jsonFile: 'dump.json',
loadJson: function() {
$.getJSON(this.jsonFile)
.done(function (data) {
this.data = data;
this.deferred.resolve(data);
}.bind(this))
.fail(this.deferred.reject);
},
getData: function() {
return this.deferred.promise();
},
};
var model = new Model();
model.getData().done(function(data) {
console.log('done')
console.log(data)
});
});
Update 1
I've just updated my code(and truncated unimportant stuff) taking @jgillich's advices into account. For the moment I feel better reading the JSON file when the object is generated which is why I outsourced the operation into separate function.$(function(){
function Model() {
this.loadJson();
};
Model.prototype = {
deferred: $.Deferred(),
jsonFile: 'dump.json',
loadJson: function() {
$.getJSON(this.jsonFile)
.done(function (data) {
this.data = data;
this.deferred.resolve(data);
}.bind(this))
.fail(this.deferred.reject);
},
getData: function() {
return this.deferred.promise();
},
};
var model = new Model();
model.getData().done(function(data) {
console.log('done')
console.log(data)
});
});
Do you have any suggestions for improvements? Thank you so much.
Do you have any suggestions for improvements? Thank you so much.
Do you have any suggestions for improvements?
Loading
default