gpt4 book ai didi

javascript - backbone.js View 不会重置

转载 作者:行者123 更新时间:2023-11-28 13:43:57 25 4
gpt4 key购买 nike

我需要使用backbone.js,我无法进入 View 中的“渲染”部分,这是我的代码:

var Vigne = {
Models:{},
Collections: {},
Views:{},
Templates:{}
}

Vigne.Models.Raisin = Backbone.Model.extend({})

Vigne.Collections.Grape = Backbone.Collection.extend({
model: Vigne.Models.Raisin,
url: "./scripts/data/vin.json",
initialize: function (){
console.log("grape initialised");
}
});

Vigne.Views.Grape= Backbone.View.extend({
initialize: function(){
_.bindAll(this,"render");
this.collection.bind("reset",this.render);
},
render: function(){
console.log("render");
console.log(this.collection.length);
}
})

Vigne.Router = Backbone.Router.extend({
routes:{
"": "defaultRoute"
},

defaultRoute: function(){
console.log("defaultRoute");
Vigne.grape = new Vigne.Collections.Grape()
new Vigne.Views.Grape ({ collection : Vigne.grape });
Vigne.grape.fetch();
console.log(Vigne.grape.length);
}
}
);

var appRouter= new Vigne.Router();
Backbone.history.start();

我希望它在调试器的控制台中显示我的集合的长度,它似乎没有重置。有什么想法吗?

编辑:

我在获取函数中添加了这个:

success: function(){
console.log(arguments);
},
error: function() {
console.log(arguments);
}
});

fetch函数成功获取json文件,但没有触发reset函数。

最佳答案

我通过将 fetch 函数中的属性设置为 true 解决了这个问题:

Vigne.grape.fetch({

reset:true,

error: function() {
console.log(arguments);
}
}

);

这本书帮助了我:http://addyosmani.github.io/backbone-fundamentals/

关于javascript - backbone.js View 不会重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15858872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com