Changeset 22692
- Timestamp:
- 11/20/2012 12:53:02 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/js/media-views.js
r22690 r22692 633 633 // `options.at`. By default, `views` are added to the end of the array. 634 634 set: function( selector, views, options ) { 635 var $selector, els, existing, method;635 var existing, next; 636 636 637 637 if ( ! _.isString( selector ) ) { … … 645 645 existing = this.get( selector ); 646 646 next = views; 647 method = options.add ? 'insert' : 'replace';648 647 649 648 if ( existing ) { … … 673 672 this._views[ selector ] = next; 674 673 675 $selector = selector ? this.view.$( selector ) : this.view.$el;676 els = _.pluck( views, 'el' );677 678 674 _.each( views, function( subview ) { 679 675 var constructor = subview.Views || media.Views, … … 683 679 }, this ); 684 680 685 if ( ! options.silent ) { 686 _.each( views, this._maybeRender, this ); 687 this[ method ]( $selector, els, options ); 688 } 681 if ( ! options.silent ) 682 this._attach( selector, views, _.extend({ ready: this._isReady() }, options ) ); 689 683 690 684 return this; … … 756 750 // Renders all subviews. Used in conjunction with `Views.detach()`. 757 751 render: function() { 758 var root = this._views['']; 759 760 _.each( this.all(), this._maybeRender, this ); 761 762 if ( root ) 763 this.replace( this.view.$el, _.pluck( root, 'el' ) ); 752 var options = { 753 ready: this._isReady() 754 }; 764 755 765 756 _.each( this._views, function( views, selector ) { 766 if ( selector ) 767 this.replace( this.view.$( selector ), _.pluck( views, 'el' ) ); 757 this._attach( selector, views, options ); 768 758 }, this ); 769 759 … … 821 811 }, 822 812 823 824 // #### Internal. Maybe render a view. 825 _maybeRender: function( view ) { 826 if ( ! view.views || view.views.rendered ) 827 return; 828 829 view.render(); 830 view.views.rendered = true; 813 // ### Trigger the ready event 814 // 815 // **Only use this method if you know what you're doing.** 816 // For performance reasons, this method does not check if the view is 817 // actually attached to the DOM. It's taking your word for it. 818 // 819 // Fires the ready event on the current view and all attached subviews. 820 ready: function() { 821 this.view.trigger('ready'); 822 823 // Find all attached subviews, and call ready on them. 824 _.chain( this.all() ).map( function( view ) { 825 return view.views; 826 }).flatten().where({ attached: true }).invoke('ready'); 827 }, 828 829 // #### Internal. Attaches a series of views to a selector. 830 // 831 // Checks to see if a matching selector exists, renders the views, 832 // performs the proper DOM operation, and then checks if the view is 833 // attached to the document. 834 _attach: function( selector, views, options ) { 835 var $selector = selector ? this.view.$( selector ) : this.view.$el, 836 managers; 837 838 // Check if we found a location to attach the views. 839 if ( ! $selector.length ) 840 return this; 841 842 managers = _.chain( views ).pluck('views').flatten().value(); 843 844 // Render the views if necessary. 845 _.each( managers, function( manager ) { 846 if ( manager.rendered ) 847 return; 848 849 manager.view.render(); 850 manager.rendered = true; 851 }, this ); 852 853 // Insert or replace the views. 854 this[ options.add ? 'insert' : 'replace' ]( $selector, _.pluck( views, 'el' ), options ); 855 856 // Set attached and trigger ready if the current view is already 857 // attached to the DOM. 858 _.each( managers, function( manager ) { 859 manager.attached = true; 860 861 if ( options.ready ) 862 manager.ready(); 863 }, this ); 864 865 return this; 866 }, 867 868 // #### Internal. Checks if the current view is in the DOM. 869 _isReady: function() { 870 var node = this.view.el; 871 while ( node ) { 872 if ( node === document.body ) 873 return true; 874 node = node.parentNode; 875 } 876 877 return false; 831 878 } 832 879 }); … … 842 889 constructor: function() { 843 890 this.views = new this.Views( this, this.views ); 891 this.on( 'ready', this.ready, this ); 844 892 Backbone.View.apply( this, arguments ); 845 893 }, … … 886 934 prepare: function() { 887 935 return this.options; 888 } 936 }, 937 938 ready: function() {} 889 939 }); 890 940 … … 968 1018 }); 969 1019 } 1020 1021 this.on( 'attach', _.bind( this.views.ready, this.views ), this ); 970 1022 }, 971 1023 … … 1061 1113 _.each(['open','close','attach','detach'], function( method ) { 1062 1114 media.view.MediaFrame.prototype[ method ] = function( view ) { 1063 this.trigger( method );1064 1115 if ( this.modal ) 1065 1116 this.modal[ method ].apply( this.modal, arguments ); 1117 this.trigger( method ); 1066 1118 return this; 1067 1119 }; … … 1593 1645 attach: function() { 1594 1646 this.$el.appendTo( this.options.container ); 1595 this. controller.trigger( 'attach', this.controller);1647 this.trigger('attach'); 1596 1648 return this; 1597 1649 }, … … 1599 1651 detach: function() { 1600 1652 this.$el.detach(); 1601 this. controller.trigger( 'detach', this.controller);1653 this.trigger('detach'); 1602 1654 return this; 1603 1655 }, … … 1605 1657 open: function() { 1606 1658 this.$el.show(); 1607 this. controller.trigger( 'open', this.controller);1659 this.trigger('open'); 1608 1660 return this; 1609 1661 }, … … 1611 1663 close: function() { 1612 1664 this.$el.hide(); 1613 this. controller.trigger( 'close', this.controller);1665 this.trigger('close'); 1614 1666 return this; 1615 1667 },
Note: See TracChangeset
for help on using the changeset viewer.