我正在尝试将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({})
,在这种情况下,始终会返回错误。
你对如何处理它有所了解吗?
谢谢,
Kai23
答案 0 :(得分: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);
}
})
}
});