3

我正在尝试将 YDN 与 Backbone.sync() 一起使用,但 « fetchAll » 函数存在问题。

例如,我有这个视图

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");
            }
        })      
    }
});

在 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;
    });
};

我想你知道我被困在哪里了。db.values("users", null, 10).done({...});是一个异步方法,例如this.users.fetch({})在这种情况下,它总是返回一个错误。

你知道如何处理它吗?

谢谢,

开23

4

1 回答 1

4

解决了 :

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);            
    });
};

和观点:

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);
        }
    })      
}
});
于 2013-05-22T15:39:55.357 回答