0

i'm creating a function that i wanna use as class on javascript. My function, will call an JSON page create with php parse json, and set the vars, but it doesen't work. May you can give me some hints? Here's the code, thx in advance:

function SiteParams(){
    $.getJSON("parse/parametri.php",function(data){
        $.each(data,function(index,value){
            this.NomeSito = value.nomesito;
            this.URLSito = value.urlsito;
            this.EmailAutore = value.email;
            this.NomeAutore = value.nomeautore;
        });
    });
}
var website = new SiteParams();
function ModuleBase(){
    $("<div/>",{id:"title", text:website.NomeSito}).appendTo("#main");
}
2
  • Although not relevant to the problem, your data structure is weird - this.sito.nome would make more sense than this.NomeSito Commented Aug 8, 2013 at 23:01
  • What does parse/parametri.php return? Can you show an example? Why are you iterating over the data? You'll only get the last result Commented Aug 8, 2013 at 23:03

3 Answers 3

1

This is a good place to use $.Deferred

function SiteParams(){
    // create a private deferred, and expose the promise:
    var d = new $.Deferred();
    this.load = d.promise();

    var that = this;
    $.getJSON("parse/parametri.php", function(data) {
        // your $.each only used one value anyway
        var value = data[0];

        // copy the data across
        that.NomeSito = value.nomesito;
        that.URLSito = value.urlsito;
        that.EmailAutore = value.email;
        that.NomeAutore = value.nomeautore;

        // resolve the promise
        d.resolve();
    });
}

var s = new SiteParams();
s.load.done(function() {
    $("<div/>", {id:"title", text: s.NomeSito}).appendTo("#main");
});
Sign up to request clarification or add additional context in comments.

1 Comment

I really have to sleep on this, this is working too but it's 100% new for me i love both solutions :)
1

You have the wrong this inside the callback to $.each (and the callback to getJSON). Try that:

function SiteParams(){
    var that = this;
    $.getJSON("parse/parametri.php",function(data){
        $.each(data,function(index,value){
            that.NomeSito = value.nomesito;
            that.URLSito = value.urlsito;
            that.EmailAutore = value.email;
            that.NomeAutore = value.nomeautore;
        });
    });
}

Note that it doesn't make much sense to loop with each if your response only contains a single object. And if it contained multiple objects, every object would overwrite the previous. So, if your response is really an array with a single item inside, you can simple use this:

function SiteParams(){
    var that = this;
    $.getJSON("parse/parametri.php",function(data){
        that.NomeSito = data[0].nomesito;
        that.URLSito = data[0].urlsito;
        that.EmailAutore = data[0].email;
        that.NomeAutore = data[0].nomeautore;
    });
}

2 Comments

This is doesen't work too, thank you :) i found the solution to other post, thank you really for your help
That answer is right about the callback. The correct answer is actually a combination of that with what I said (see Eric's comment).
1

getJSON is asynchronous, so you need to pass a callback function. Try this:

function SiteParams(cb){
    $.getJSON("parse/parametri.php",function(data){
        $.each(data,function(index,value){
            this.NomeSito = value.nomesito;
            this.URLSito = value.urlsito;
            this.EmailAutore = value.email;
            this.NomeAutore = value.nomeautore;
            cb(this);
        });
    });
}
new SiteParams(ModuleBase);
function ModuleBase(website){
    $("<div/>",{id:"title", text:website.NomeSito}).appendTo("#main");
}

4 Comments

This is work perfectly, never thought about call back function i'm littlebit new on this :)
Note that here value == this, which is probably unintentional
Great, glad I could help! The other reply also makes sense in combination with this one -- you should use a different "this" object or else you might be putting those extra properties on the global ("window") object. It will work, but it's better to encapsulate. See javascript.crockford.com/private.html for the "var that = this" pattern.
Agreed, it would make much more sense for this to just be a function (and not call it with "new"). Then store the values on some other variable (not "this") and pass it into the callback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.