简体   繁体   中英

How to access a specific view in backbone.js?

I have a backbone viev which generates a list item view. In the render function it goes through the collection and generates each of the sub item views with some standard code as follows:

render: function () {
    _(this.collection.models).each(function(item){
        $this.appendItem(item);
     }, this);          
},

I would like to know how to access a specific view from the item list, say at position 0 or something. I want to be able to trigger a function from the item list view for that specific item.

Well, that depends on how the appendItem function is implemented, I guess is in there where you build the SubViews .

You can store each created SubView in an Array so the Array will offer you a way to manipulate the SubViews.

But if I can offer a piece of advice I would say that this is a code smell from the begining. Instead of trying to manipulate a concrete SubView you can manipulate the Model which is associated with the SubView and make the SubView to be listening to this change.

Then you will start to be thinking in manipulating Models instead of Views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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