简体   繁体   English

渲染骨干集合中每个模型的视图

[英]Rendering views for each model in a backbone collection

I'm working on a toy backbone.js application, a library application to perform CRUD operations on a library. 我正在研究toybone.js应用程序,这是一个在库上执行CRUD操作的库应用程序。 Here is the book model and the library collection (of books) 这是书籍模型和(书籍的)图书馆藏书

var Book = Backbone.Model.extend({
    url: function() {
        return '/api/books' + this.get('id');
    }
});

var Library = Backbone.Collection.extend({
    model : Book,
    url : '/api/books'
});

This seems pretty straightforward. 这似乎很简单。 Next, I obviously want to be able to show the book, so I have a BookView... 接下来,我显然希望能够显示该书,所以我有一个BookView ...

var BookView = Backbone.View.extend({
    tagName: 'li',
    render: function() {
        this.$el.html(this.model.get('author'));
        $('#list-of-books').append(this.$el);
    }
});

all the render method does is append an li to the end of an unordered list with an id of list-of-books that is in my html. 所有render方法所做的就是将li附加到无序列表的末尾,其ID为我的html中的书列表。

Next, unsurprisingly, if I add the following code, I get a list with one item (the name of the author of the book with id=4) 接下来,毫不奇怪,如果添加以下代码,则会得到一个包含一个项目的列表(id = 4的书作者的名字)

var a_book = new Book();
a_book.url = '/api/books/4';
a_book.fetch({
    success: function() {
        var bookView = new BookView({ model: a_book });
        bookView.render();
    }
});

Here's where I don't understand. 这是我不明白的地方。 I add the following code and nothing happens: 我添加以下代码,但没有任何反应:

var some_books = new Library();
some_books.fetch();
some_books.forEach(function(book) {
    alert('why is this function not being run');
    var view = new BookView({ model: book });
    view.render();
});

For some reason, I can't even get that loop code to run, I don't even see that alert pop out to the screen. 由于某种原因,我什至无法运行该循环代码,甚至看不到警报弹出到屏幕上。 Let me know if you understand what's wrong with this code and how I can make the list render properly. 让我知道您是否理解此代码有什么问题以及如何正确显示列表。 Thanks 谢谢

You are calling .render inside fetch. 您正在提取中调用.render。 Hence the execution stops over there itself. 因此,执行本身就在那里停止。 For loop wont run after fetch as it has returned already. for循环在获取后不会运行,因为它已经返回。

.fetch() is an asynchronous call, so you may not have the collection data populated when you are calling .forEach(). .fetch()是一个异步调用,因此您在调用.forEach()时可能没有填充收集数据。

You should either call the rendering code in the success callback or (even better) render the view on events (reset,...) on the collection. 您应该在成功回调中调用呈现代码,或者(甚至更好)在集合上的事件(reset,...)上呈现视图。

BTW, you have a typo in the url() function: 顺便说一句,您在url()函数中有一个错字:

url: function() {
    return '/api/books/' + this.get('id');
}

Why are you defining the url function for the Book?, since you use the model attribute in the Library collection, url for the Book will be set correctly by default. 为什么要为Book定义url函数?由于使用了Library集合中的model属性,因此默认情况下将正确设置Book的url。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM