主干收集事件未触发。我错过了什么吗?
我正在尝试制作一个预加载器,但在第一步中就陷入了困境。在使用 jquery 和“原始”js 之前,我已经构建了一个不错的脚本,基本上发生的情况是我有一个名为 img/ui 的文件夹和一个服务器端脚本,该脚本仅提供该文件夹的 JSON 转储。该请求是 /preload js,然后将其排队并根据具有超时和超时的加载事件过程一一加载它们。错误处理程序。 我正在尝试将其移植到 Backbone。我认为的模式是一个集合,它加载 JSON,为每个资产构建一组模型,然后将单个视图附加到集合以显示队列的状态……很简单。
但我已经卡住了..首先我必须手动获取JSON,否则它不会做任何事情..很好..完成,其次即使加载了JSON,它也不会触发解析方法(或任何其他方法):
var PreloaderCollection = Backbone.Collection.extend({
model:PreloaderModel,
url:"/preload",
events: {
"change":"parse"
},
initialize: function()
{
_.bindAll(this);
this.fetch(this.url);
},
setup: function(args)
{
},
update: function(args)
{
},
parse:function(args){
log(args)
},
remove: function(args)
{
}
});
我是真的开始对 Backbone 感到沮丧,这是我的第一个主要项目,尽管阅读了每个教程并完全浏览了源代码,但模式和功能似乎存在很多矛盾。
编辑:
这是最后的手段,感觉很肮脏,但这是我“绕过”这个问题的方法。 我基本上用我自己的方法覆盖了 fetch 函数,现在可以工作了,但是......嗯,
var PreloaderCollection = Backbone.Collection.extend({
url:"/preload",
events: {
"reset":"parse"
},
initialize: function()
{
log("initing preloader collection")
_.bindAll(this);
this.bind("change",this.parse)
this.fetch(this.url);
},
fetch:function(args){
$.getJSON(
args,
this.parse
)
},
setup: function(args)
{
},
update: function(args)
{
},
parse:function(args){
log("parse",args)
},
remove: function(args)
{
}
});
I'm trying to make a preloader and getting caught at step one with backbone. I've built a nice one before using jquery and also with 'raw' js basically what happens is that I have a folder called img/ui and a server side script that just gives a JSON dump of that folder. This request is /preload the js then queues this and loads them one by one based upon a process of load events with timeouts & error handlers.
What I'm trying to is port this to Backbone. The pattern I thought was a collection which loads the JSON build a set of models for each of the assets then a single view attached to the collection to display the status of the queue...... simple.
But I'm already stuck.. first I have to manually fetch the JSON or it wont do anything.. fine.. done, second even when the JSON is loaded it wont fire the parse method (or any other):
var PreloaderCollection = Backbone.Collection.extend({
model:PreloaderModel,
url:"/preload",
events: {
"change":"parse"
},
initialize: function()
{
_.bindAll(this);
this.fetch(this.url);
},
setup: function(args)
{
},
update: function(args)
{
},
parse:function(args){
log(args)
},
remove: function(args)
{
}
});
I'm really starting to get frustrated with Backbone, It's my first major project and despite reading about every tutorial and fully going through the source there seems to be so much contradiction about the pattern and capabilities.
EDIT:
This was a last resort and feels very dirty but here's how I 'bypassed' the issue.
I essentially overrode the fetch function with my own like so, which now works but... hmm,
var PreloaderCollection = Backbone.Collection.extend({
url:"/preload",
events: {
"reset":"parse"
},
initialize: function()
{
log("initing preloader collection")
_.bindAll(this);
this.bind("change",this.parse)
this.fetch(this.url);
},
fetch:function(args){
$.getJSON(
args,
this.parse
)
},
setup: function(args)
{
},
update: function(args)
{
},
parse:function(args){
log("parse",args)
},
remove: function(args)
{
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用了错误的方式绑定到您的事件:)
通过事件的哈希值,您可以在视图上声明 jquery 需要绑定到 DOM 中的元素的所有事件。
在模型或集合中,您绑定到重置/更改/错误事件,如下所示:
在此处查看有关事件绑定的更多信息:http: //documentcloud.github.com/backbone/#Events-bind
查看有关您尝试使用的 delegateEvents 的更多信息,但仅用于 DOM 元素事件绑定的视图中:http://documentcloud.github.com/backbone/#View-delegateEvents
查看 jsfiddle 的工作版本:http://jsfiddle.net/saelfaer/kt2KJ/1/
you are using the wrong way of binding to your events :)
whith the event's hash, you declare all events jquery need to bind to elements in the DOM, on your view.
in a model, or collection you bind to reset / change / error events like this:
see more info on eventbinding here: http://documentcloud.github.com/backbone/#Events-bind
see more info on the delegateEvents you were trying to use, but are only meant to be used in a view for DOM element event binding: http://documentcloud.github.com/backbone/#View-delegateEvents
see jsfiddle for a working version: http://jsfiddle.net/saelfaer/kt2KJ/1/
获取后监听的事件被重置。因此,对于 actin 来说,你必须写:
事件:{
“重置”:“解析”
fetch
的文档可以在主干页面上找到:
http://documentcloud.github.com/backbone/#Collection-fetch
The event to listen for after fetching is reset. So for actin on that you will have to write:
events: {
"reset":"parse"
}
The documentation for fetch can be found on the backbone page:
http://documentcloud.github.com/backbone/#Collection-fetch