Changeset 22655
- Timestamp:
- 11/19/2012 02:43:10 AM (11 years ago)
- Location:
- trunk/wp-includes/js
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/js/media-models.js
r22654 r22655 359 359 360 360 validate: function( attachment, options ) { 361 var valid = this.validator( attachment ), 362 hasAttachment = !! this.getByCid( attachment.cid ); 363 361 364 // Only retain the `silent` option. 362 365 options = { … … 364 367 }; 365 368 366 return this[ this.validator( attachment ) ? 'add' : 'remove' ]( attachment, options ); 369 if ( ! valid && hasAttachment ) 370 this.remove( attachment, options ); 371 else if ( valid && ! hasAttachment ) 372 this.add( attachment, options ); 373 374 return this; 375 }, 376 377 validateAll: function( attachments ) { 378 _.each( attachments.models, function( attachment ) { 379 this.validate( attachment, { silent: true }); 380 }, this ); 381 382 return this; 367 383 }, 368 384 369 385 observe: function( attachments ) { 370 attachments.on( 'add change', this.validate, this ); 386 this.observers = this.observers || []; 387 this.observers.push( attachments ); 388 389 attachments.on( 'add change remove', this._validateHandler, this ); 390 attachments.on( 'reset', this._validateAllHandler, this ); 391 392 this.validateAll( attachments ); 393 return this; 371 394 }, 372 395 373 396 unobserve: function( attachments ) { 374 attachments.off( 'add change', this.validate, this ); 397 if ( attachments ) { 398 attachments.off( null, null, this ); 399 this.observers = _.without( this.observers, attachments ); 400 401 } else { 402 _.each( this.observers, function( attachments ) { 403 attachments.off( null, null, this ); 404 }, this ); 405 delete this.observers; 406 } 407 408 return this; 409 }, 410 411 _validateHandler: function( attachment, attachments, options ) { 412 return this.validate( attachment, options ); 413 }, 414 415 _validateAllHandler: function( attachments, options ) { 416 return this.evaluateAll( attachments, options ); 375 417 }, 376 418 … … 519 561 }; 520 562 521 // Observe the central ` Attachments.all` model to watch for new522 // matches for the query.563 // Observe the central `wp.Uploader.queue` collection to watch for 564 // new matches for the query. 523 565 // 524 566 // Only observe when a limited number of query args are set. There … … 526 568 // false positives in those queries. 527 569 allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type' ]; 528 if ( _( this.args ).chain().keys().difference( allowed ).isEmpty().value() )529 this.observe( Attachments.all);570 if ( wp.Uploader && _( this.args ).chain().keys().difference( allowed ).isEmpty().value() ) 571 this.observe( wp.Uploader.queue ); 530 572 }, 531 573 … … 733 775 }); 734 776 735 /**736 * wp.media.model.Composite737 *738 * Creates a model that can simultaneously pull from two or more collections.739 */740 media.model.Composite = Attachments.extend({741 initialize: function( models, options ) {742 this.observe( this, { silent: true });743 Attachments.prototype.initialize.apply( this, arguments );744 },745 746 evaluate: function( attachment, options ) {747 var valid = this.validator( attachment ),748 hasAttachment = !! this.getByCid( attachment.cid );749 750 if ( ! valid && hasAttachment )751 this.remove( attachment, options );752 else if ( valid && ! hasAttachment )753 this.add( attachment, options );754 755 return this;756 },757 758 validator: function() {759 return true;760 },761 762 evaluateAll: function( attachments, options ) {763 _.each( attachments.models, function( attachment ) {764 this.evaluate( attachment, { silent: true });765 }, this );766 767 return this;768 },769 770 observe: function( attachments, options ) {771 var silent = options && options.silent;772 this.observers = this.observers || [];773 this.observers.push( attachments );774 775 attachments.on( 'add remove', silent ? this._evaluateSilentHandler : this._evaluateHandler, this );776 attachments.on( 'reset', silent ? this._evaluateAllSilentHandler : this._evaluateAllHandler, this );777 778 this.evaluateAll( attachments, options );779 return this;780 },781 782 unobserve: function( attachments ) {783 if ( attachments ) {784 attachments.off( null, null, this );785 this.observers = _.without( this.observers, attachments );786 787 } else {788 _.each( this.observers, function( attachments ) {789 attachments.off( null, null, this );790 }, this );791 delete this.observers;792 }793 794 return this;795 },796 797 _evaluateHandler: function( attachment, attachments, options ) {798 return this.evaluate( attachment, options );799 },800 801 _evaluateAllHandler: function( attachments, options ) {802 return this.evaluateAll( attachments, options );803 },804 805 _evaluateSilentHandler: function( attachment, attachments, options ) {806 return this.evaluate( attachment, _.defaults({ silent: true }, options ) );807 },808 809 _evaluateAllSilentHandler: function( attachments, options ) {810 return this.evaluateAll( attachments, _.defaults({ silent: true }, options ) );811 }812 });813 814 777 }(jQuery)); -
trunk/wp-includes/js/media-views.js
r22651 r22655 388 388 389 389 // Create a composite library in its place. 390 composite = new media.model. Composite( null, {390 composite = new media.model.Attachments( null, { 391 391 props: _.pick( original.props.toJSON(), 'order', 'orderby' ) 392 392 }); … … 2411 2411 _.each(['add','remove'], function( method ) { 2412 2412 this.collection.on( method, function( attachment, attachments, options ) { 2413 console.log( method, 'attachment', attachment.id, 'at', options.index ); 2413 2414 this[ method ]( attachment, options.index ); 2414 2415 }, this );
Note: See TracChangeset
for help on using the changeset viewer.