【问题标题】:Javascript - BackboneJS - Backbone.sync override - YDN - Asynchronous errorJavascript - BackboneJS - Backbone.sync 覆盖 - YDN - 异步错误
【发布时间】:2013-05-22 15:16:18
【问题描述】:

我正在尝试将 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

【问题讨论】:

    标签: javascript backbone.js backbone-views ydn-db


    【解决方案1】:

    已解决:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2014-07-21
      • 1970-01-01
      相关资源
      最近更新 更多