Backbone.js 点击事件无法一致工作

发布于 2024-12-11 11:14:18 字数 2066 浏览 0 评论 0原文

我正在使用rails api开发backbone.js应用程序,它提供如下所示的json:

{
    "name": "dan brown",
    "genre": "fiction",
    "books": [{
        "title": "Da vinci Code",
        "price": "50"
    },
    {
        "title": "Some other name",
        "price": "45"
    }],

    "name": "Author1",
    "genre": "comedy",
    "books": [{
        "title": "asdfasdfdsf",
        "price": "50"
    },
    {
        "title": "asdfdsf name",
        "price": "45"
    }]
}

Todo: 1. 在页面左侧显示作者列表。 2.当用户点击作者时,应该显示相应的书籍,并带有add_to_cart的链接 3. 最后,当用户单击该书时,它应该显示有关该书的更多详细信息

进度: 1. 我能够使用backbone.js 获取作者列表并显示在左侧(作者.fetch()) 2. 当用户点击时,我能够显示该特定作者的书籍。

问题:现在,当用户第一次点击 AUTHOR1 时,它会显示该作者的书籍但是,在点击其他作者之后,如果用户点击相同的 Author1 再次没有显示任何书籍。似乎用户 Backbone 作者模型在单击一次后丢失了单击事件...并且我希望单击事件在作者身上持续存在...

该怎么办?

去哪里做?

非常感谢任何帮助和建议

在AuthorView中

events: {
    "click .author": "select"
},

select: function() {
    this.collection.trigger('select', this.model);
    //console.log(this.model);
}

在BookListView中:

this.collection.bind('add', this.renderBook);

this.book = this.options.book;

this.authors = this.options.authors;
this.authors.bind('select', this.queueBooks);

queueBooks: function(author) {
    this.collection.add(author);
}

我猜测问题出在queueBooks:方法中,当将模型添加到 集合它会触发一个添加事件,然后用于通过 renderBook 回调渲染书籍...所以我认为如果模型已经存在于集合中,它不会再次触发添加事件,从而不会渲染书籍...怎么解决这个问题啊????!!!!!!!!!!!!

编辑:正如Derick建议的

this.authors = this.options.authors;
this.authors.bind('select', this.renderBooks);

-这完美地工作但是这样在集合中不会生成添加事件,因为没有添加模型。 ...我想要 BookList 集合中的书籍模型的原因是在 Booklist 集合上有一个过滤器,以便可以根据价格和其他标准过滤它们,有没有办法实现这些功能?????? ????

I am working on a backbone.js application with rails api which provides the json as shown:

{
    "name": "dan brown",
    "genre": "fiction",
    "books": [{
        "title": "Da vinci Code",
        "price": "50"
    },
    {
        "title": "Some other name",
        "price": "45"
    }],

    "name": "Author1",
    "genre": "comedy",
    "books": [{
        "title": "asdfasdfdsf",
        "price": "50"
    },
    {
        "title": "asdfdsf name",
        "price": "45"
    }]
}

Todo: 1. TO display the list of authors on the left side of the page.
2. When the user clicks on the author, corresponding books should be displayed with a link to add_to_cart
3. Finally when the user clicks on the book, it should display more details about that book

Progress: 1. I am able to get the list of authors using backbone.js and display on the left side (authors.fetch())
2. And i was able to get the books of that particular author to show up when user clicks.

Problem: Now When the user clicks on the AUTHOR1 for the first time it'll display the books of that author BUT after clicking on some other author, if the user clicks on the same Author1 again no books are displayed.. It seems user Backbone author model losing the click event after it's clicked once....And I want the to click event to persist on the authors...

WHAT TO DO ?

WHERE TO DO ?

Any Help and Suggestions are highly appreciated

In AuthorView

events: {
    "click .author": "select"
},

select: function() {
    this.collection.trigger('select', this.model);
    //console.log(this.model);
}

In BookListView:

this.collection.bind('add', this.renderBook);

this.book = this.options.book;

this.authors = this.options.authors;
this.authors.bind('select', this.queueBooks);

queueBooks: function(author) {
    this.collection.add(author);
}

I'm guessing that the problem lies in the queueBooks: method, when a model is added to the
collection it fires a add event which then is used to render the books through renderBook callback...So i think if the model already exists in the collection it doesn't fire the add the event again thereby not rendering the books....How to solve this??????!!!!!!!!!!!!!!!

EDIT: As suggested by Derick-

this.authors = this.options.authors;
this.authors.bind('select', this.renderBooks);

This works perfectly BUT this way no add event is generated in the collection as no model is bieng added.....the reason i want the book models in the BookList collection is to have a filter on the booklist collection so that they can be filtered based on prices, and other criteria, Is there any way to implement these features?????????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷子口的你 2024-12-18 11:14:18

我猜测问题出在queueBooks:方法中,当
模型被添加到集合中,它会触发一个添加事件,然后
用于通过 renderBook 回调渲染书籍...所以我想如果
该模型已存在于集合中,它不会触发添加
再次发生事件,从而不会渲染书籍

那是正确的。您只能将模型添加到集合中一次。当您第二次尝试添加它时,它会抛出 JavaScript 错误。

您已经拥有所需的一切,您只是听到了错误的事件并以错误的顺序做事。您不需要将模型添加到集合中来显示它,而是需要侦听模型的 select 事件,并使用该事件来显示它。

this.authors = this.options.authors;
this.authors.bind('select', this.renderBook);

I'm guessing that the problem lies in the queueBooks: method, when a
model is added to the collection it fires a add event which then is
used to render the books through renderBook callback...So i think if
the model already exists in the collection it doesn't fire the add the
event again thereby not rendering the books

That is correct. You can only add a model to a collection once. It will throw a JavaScript error when you try to add it the second time.

You already have everything you need in place, you're just listening to the wrong events and doing things in the wrong order. Instead of adding a model to the collection to display it, you need to listen to the select event of the model, and use that to display it.

this.authors = this.options.authors;
this.authors.bind('select', this.renderBook);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文