[wp-trac] [WordPress Trac] #30505: Backbone subviews cannot be attached directly to DOM elements

WordPress Trac noreply at wordpress.org
Tue Dec 2 02:58:03 UTC 2014


#30505: Backbone subviews cannot be attached directly to DOM elements
----------------------------+------------------------------
 Reporter:  rmccue          |       Owner:
     Type:  defect (bug)    |      Status:  new
 Priority:  normal          |   Milestone:  Awaiting Review
Component:  Administration  |     Version:
 Severity:  normal          |  Resolution:
 Keywords:                  |     Focuses:  javascript
----------------------------+------------------------------

Comment (by rmccue):

 Replying to [comment:1 adamsilverstein]:
 > Can you give an example of how you are trying to "attach subviews
 directly to DOM elements" - maybe a code snippet?

 Say for example that you have a list of elements already on the page:

 {{{
 <div id="component-list">
         <div class="component"></div>
         <div class="component"></div>
         <div class="component"></div>
 </div>
 }}}

 Typically with Backbone, you can attach these existing elements to views:

 {{{
 jQuery('#component-list .component').each( function () {
         var myview = new Backbone.View( { el: this } );
 } );
 }}}

 However, if `#component-list` is itself a view, and you want to attach
 each
 subelement as a subview, you can't do the same. Selector needs a unique
 string
 selector for each of the `.component` elements that you want to attach.

 If each of the elements has an ID, this isn't as hard:

 {{{
         initialize: function () {
                 var subviews = this.views;
                 this.$('.component').each(function () {
                         // If each of the elements has an ID, you can use:
                         var selector = '#' . this.id;
                         subviews.add( selector, new Backbone.View( { el:
 this } ) );
                 });
         }
 }}}

 I don't want (or otherwise need) IDs on each of these though. Instead of
 `selector`, it'd be nice to be able to pass the element itself (or just a
 string selector).

 > I don't understand the use case and also: I'm confused - are we talking
 wp.Backbone.Subviews:
 https://core.trac.wordpress.org/browser/tags/4.0.1/src/wp-includes/js/wp-
 backbone.js#L12 or wp.Bckbone.View:
 https://core.trac.wordpress.org/browser/tags/4.0.1/src/wp-includes/js/wp-
 backbone.js#L339

 This is an issue in `wp.Backbone.Subviews`, which is used by
 `wp.Backbone.View`. The original description was a little unclear here,
 sorry!

--
Ticket URL: <https://core.trac.wordpress.org/ticket/30505#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list