Search code examples
javascriptbackbone.jsbackbone-viewsydn-db

Javascript - BackboneJS - Backbone.sync override - YDN - Asynchronous error


I'm trying to use YDN with Backbone.sync(), but I've a problem with the « fetchAll » function.

For exemple, I've this View

var UserListView = Backbone.View.extend({
    el: ".page",


    initialize: function() {
        this.users = new UserCollection();
    },

    render: function() {
        var that = this;
        this.users.fetch({
            success:function(users){
                var template = _.template($('#user-list-template').html(), {users : users.models});
                that.$el.html(template);
            },
            error: function(users){
                console.log("error");
            }
        })      
    }
});

And in BackboneSync :

switch (method) {
    case "read":
        resp = model.id ? get(model) : all(model);
        break;
    case "create":
        resp = save(model);
        break;
    case "update":
        resp = save(model);
        break;
    case "delete":
        resp = remove(model);
        break;
}

function all(collection) {
    db.values("users", null, 10).done(function(records) {
        $.each(records, function(index, val) {
            collection.add(records);
        });
        return collection;
    });
};

I think you know where I'm stuck. db.values("users", null, 10).done({...}); is an async method, such as this.users.fetch({}) which, in that case, always return an error.

Do you have an idea on how to handle it ?

Thanks,

Kai23


Solution

  • Solved :

    switch (method) {
        case "read":
            model.id ? get(model) : all(model);
            break;
        case "create":
            resp = save(model);
            break;
        case "update":
            resp = save(model);
            break;
        case "delete":
            resp = remove(model);
            break;
    }
    
    function all(collection) {
        console.log("je rentre");
        db.values("users", null, 10).done(function(records) {
            options.success(records);            
        });
    };
    

    And the view :

    var UserListView = Backbone.View.extend({
    el: ".page",
    
    
    initialize: function() {
        this.users = new UserCollection();
    },
    
    render: function() {
        var that = this;
        this.users.fetch({
            success:function(users){
                console.log(users.models);
                var template = _.template($('#user-list-template').html(), {users : users.models});
                that.$el.html(template);
            }
        })      
    }
    });