1

i have the following javascript object:

var Playlist = function(callback) {
    $.ajax({
        url:'/playlist'
    })
    .done(function(data) {
        this.html = data;
        callback({},this);
    })
    .fail(function(err) {
        callback(err,{});
    });
};

for this object, i declared a prototype method:

Playlist.prototype = {
    render : function() {
        $('#main').html(this.html);
    }
};

now, when i create a new object und try to call the 'render' function like this

function renderPlaylist() {
    var playlist = new Playlist(function(err, obj) {
        obj.render();
    });
}

renderPlaylist();

i get a 'undefined is not a function'. it seems not to know the render function, which i declared in the prototype. what am i missing here? thanks for help.

2

1 Answer 1

1

with the help of Volune, i changed my constructor to

var Playlist = function(callback) {
    var playlist = this;
    $.ajax({
        url:'/mpdplaylist'
    })
    .done(function(data) {
        playlist.html = data;
        callback(null,playlist);
    })
    .fail(function(err) {
        callback(err,{});
    });
};

now it works. thank you!!

edit: as Volune pointed out, i replaced the empty object for the error with null in the 'done' callback function

Sign up to request clarification or add additional context in comments.

1 Comment

If you're using callbacks the same way as NodeJS, you should do callback(null,playlist);. This way the callback can more easily check if there were an error.

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.