Make WordPress Core

Changeset 22662


Ignore:
Timestamp:
11/19/2012 06:02:00 AM (14 years ago)
Author:
koopersmith
Message:

Media: Add default render method to views.

  • Use default render method in the Frame view.
  • Rename Views.attach to Views.insert.
  • Add Views.all to retrieve all subviews.
  • Add Views.detach to detach all subviews.
  • Detect whether views are going to be reused in View.set and detach them instead of calling dispose.

see #21390.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/js/media-views.js

    r22660 r22662  
    575575
    576576        _.extend( media.Views.prototype, {
     577                all: function() {
     578                        return _.flatten( this._views );
     579                },
     580
    577581                get: function( selector ) {
    578582                        selector = selector || '';
     
    593597                        existing = this.get( selector );
    594598                        next     = views;
    595                         method   = add ? 'attach' : 'replace';
     599                        method   = add ? 'insert' : 'replace';
    596600
    597601                        if ( existing ) {
     
    602606                                                next = existing.splice.apply( existing, [ options.at, 0 ].concat( views ) );
    603607                                } else {
    604                                         this.unset( selector );
    605                                         _.invoke( existing, 'dispose' );
     608                                        _.each( next, function( view ) {
     609                                                view.__detach = true;
     610                                        });
     611
     612                                        _.each( existing, function( view ) {
     613                                                if ( view.__detach )
     614                                                        view.$el.detach();
     615                                                else
     616                                                        view.dispose();
     617                                        });
     618
     619                                        _.each( next, function( view ) {
     620                                                delete view.__detach;
     621                                        });
    606622                                }
    607623                        }
     
    635651                        }
    636652
    637                         views = _.isArray( views ) ? views : [ views ];
    638 
    639                         if ( existing = this.get( selector ) )
    640                                 this._views[ selector ] = _.difference( existing, views );
    641 
     653                        if ( existing = this.get( selector ) ) {
     654                                views = _.isArray( views ) ? views : [ views ];
     655                                this._views[ selector ] = views.length ? _.difference( existing, views ) : [];
     656                        }
     657
     658                        return this;
     659                },
     660
     661                detach: function() {
     662                        $( _.pluck( this.all(), 'el' ) ).detach();
    642663                        return this;
    643664                },
     
    661682                        delete this.selector;
    662683
    663                         _.chain( this._views ).flatten().invoke('dispose');
     684                        _.invoke( this.all(), 'dispose' );
    664685                        this._views = [];
     686                        return this;
    665687                },
    666688
     
    670692                },
    671693
    672                 attach: function( $target, els, options ) {
     694                insert: function( $target, els, options ) {
    673695                        var at = options && options.at,
    674696                                $children;
     
    718740                        this.dispose();
    719741                        return Backbone.View.prototype.remove.apply( this, arguments );
     742                },
     743
     744                render: function() {
     745                        var options;
     746
     747                        this.views.detach();
     748
     749                        if ( this.template ) {
     750                                options = this.prepare ? this.prepare() : {};
     751                                this.trigger( 'prepare', options );
     752                                this.$el.html( this.template( options ) );
     753                        }
     754
     755                        this.views.render();
     756                        return this;
     757                },
     758
     759                prepare: function() {
     760                        return this.options;
    720761                }
    721762        });
     
    754795                                model.frame = this;
    755796                        }, this );
    756                 },
    757 
    758                 render: function() {
    759                         if ( ! this.template )
    760                                 return;
    761 
    762                         this.$el.html( this.template( this.options ) );
    763                         this.views.render();
    764                         return this;
    765797                },
    766798
Note: See TracChangeset for help on using the changeset viewer.