Make WordPress Core

Changeset 22888


Ignore:
Timestamp:
11/28/2012 02:03:31 AM (12 years ago)
Author:
nacin
Message:

Ensure attachment modal details are saved before the attachment details view is destroyed. props koopersmith. fixes #22593.

File:
1 edited

Legend:

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

    r22882 r22888  
    815815            }
    816816
     817            views = views || [];
     818
    817819            if ( existing = this.get( selector ) ) {
    818820                views = _.isArray( views ) ? views : [ views ];
     
    821823
    822824            if ( ! options || ! options.silent )
    823                 _.invoke( views, 'dispose', { silent: true });
     825                _.invoke( views, 'dispose' );
    824826
    825827            return this;
     
    10151017            var options;
    10161018
     1019            if ( this.prepare )
     1020                options = this.prepare();
     1021
    10171022            this.views.detach();
    10181023
    10191024            if ( this.template ) {
    1020                 options = this.prepare ? this.prepare() : {};
     1025                options = options || {};
    10211026                this.trigger( 'prepare', options );
    10221027                this.$el.html( this.template( options ) );
     
    24342439        },
    24352440
    2436         destroy: function() {
    2437             this.model.off( null, null, this );
    2438             this.remove();
     2441        dispose: function() {
     2442            this.updateAll();
     2443            media.View.prototype.dispose.apply( this, arguments );
     2444            return this;
    24392445        },
    24402446
     
    24632469                options.size = this.imageSize();
    24642470
     2471            this.views.detach();
    24652472            this.$el.html( this.template( options ) );
    24662473
     
    24752482                this.select();
    24762483
     2484            this.views.render();
    24772485            return this;
    24782486        },
     
    25722580        },
    25732581
     2582        updateAll: function() {
     2583            var $settings = this.$('[data-setting]'),
     2584                model = this.model,
     2585                changed;
     2586
     2587            changed = _.chain( $settings ).map( function( el ) {
     2588                var $input = $('input, textarea, select, [value]', el ),
     2589                    setting, value;
     2590
     2591                if ( ! $input.length )
     2592                    return;
     2593
     2594                setting = $(el).data('setting');
     2595                value = $input.val();
     2596
     2597                // Record the value if it changed.
     2598                if ( model.get( setting ) !== value )
     2599                    return [ setting, value ];
     2600            }).compact().object().value();
     2601
     2602            if ( changed )
     2603                model.save( changed );
     2604        },
     2605
    25742606        removeFromLibrary: function( event ) {
    25752607            // Stop propagation so the model isn't selected.
     
    26152647        tagName:   'ul',
    26162648        className: 'attachments',
    2617         template:  media.template('attachments-css'),
     2649
     2650        cssTemplate: media.template('attachments-css'),
    26182651
    26192652        events: {
     
    26322665            });
    26332666
    2634             _.each(['add','remove'], function( method ) {
    2635                 this.collection.on( method, function( attachment, attachments, options ) {
    2636                     this[ method ]( attachment, options.index );
    2637                 }, this );
     2667            this._viewsByCid = {};
     2668
     2669            this.collection.on( 'add', function( attachment, attachments, options ) {
     2670                this.views.add( this.createAttachmentView( attachment ), {
     2671                    at: options.index
     2672                });
     2673            }, this );
     2674
     2675            this.collection.on( 'remove', function( attachment, attachments, options ) {
     2676                var view = this._viewsByCid[ attachment.cid ];
     2677                delete this._viewsByCid[ attachment.cid ];
     2678
     2679                if ( view )
     2680                    view.remove();
    26382681            }, this );
    26392682
     
    26652708                $css.remove();
    26662709
    2667             media.view.Attachments.$head().append( this.template({
     2710            media.view.Attachments.$head().append( this.cssTemplate({
    26682711                id:     this.el.id,
    26692712                edge:   this.edge(),
     
    27402783        },
    27412784
    2742         render: function() {
    2743             // If there are no elements, load some.
    2744             if ( ! this.collection.length ) {
     2785        createAttachmentView: function( attachment ) {
     2786            var view = new this.options.AttachmentView({
     2787                controller: this.controller,
     2788                model:      attachment,
     2789                collection: this.collection,
     2790                selection:  this.options.selection
     2791            });
     2792
     2793            return this._viewsByCid[ attachment.cid ] = view;
     2794        },
     2795
     2796        prepare: function() {
     2797            // Create all of the Attachment views, and replace
     2798            // the list in a single DOM operation.
     2799            if ( this.collection.length ) {
     2800                this.views.set( this.collection.map( this.createAttachmentView, this ) );
     2801
     2802            // If there are no elements, clear the views and load some.
     2803            } else {
     2804                this.views.unset();
    27452805                this.collection.more().done( this.scroll );
    2746                 this.$el.empty();
    2747                 return this;
    27482806            }
    2749 
    2750             // Otherwise, create all of the Attachment views, and replace
    2751             // the list in a single DOM operation.
    2752             this.$el.html( this.collection.map( function( attachment ) {
    2753                 return new this.options.AttachmentView({
    2754                     controller: this.controller,
    2755                     model:      attachment,
    2756                     collection: this.collection,
    2757                     selection:  this.options.selection
    2758                 }).render().$el;
    2759             }, this ) );
    2760 
    2761             return this;
    27622807        },
    27632808
     
    27662811            // threshold to query for additional attachments.
    27672812            this.scroll();
    2768         },
    2769 
    2770         add: function( attachment, index ) {
    2771             var view, children;
    2772 
    2773             view = new this.options.AttachmentView({
    2774                 controller: this.controller,
    2775                 model:      attachment,
    2776                 collection: this.collection,
    2777                 selection:  this.options.selection
    2778             }).render();
    2779 
    2780             children = this.$el.children();
    2781 
    2782             if ( children.length > index )
    2783                 children.eq( index ).before( view.$el );
    2784             else
    2785                 this.$el.append( view.$el );
    2786         },
    2787 
    2788         remove: function( attachment, index ) {
    2789             var children = this.$el.children();
    2790             if ( children.length )
    2791                 children.eq( index ).detach();
    27922813        },
    27932814
Note: See TracChangeset for help on using the changeset viewer.