Make WordPress Core

Changeset 21900


Ignore:
Timestamp:
09/18/2012 10:19:05 PM (12 years ago)
Author:
koopersmith
Message:

Ensure the Attachments model properties are correctly set for Query collections.

After shifting sorting and searching logic from the Query collection in [21898], it became apparent that Query collections should also have an accurate props model, as the model controls the aforementioned searching and sorting.

see #21921, #21809, and #21390.

File:
1 edited

Legend:

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

    r21898 r21900  
    441441        }
    442442    }, {
    443         defaultArgs: {
    444             posts_per_page: 40,
     443        defaultProps: {
    445444            orderby:       'date',
    446445            order:         'DESC'
    447446        },
    448447
     448        defaultArgs: {
     449            posts_per_page: 40
     450        },
     451
    449452        orderby: {
    450             allowed: [ 'name', 'author', 'date', 'title', 'modified', 'parent', 'ID' ],
    451             keymap: {
     453            allowed:  [ 'name', 'author', 'date', 'title', 'modified', 'uploadedTo', 'id' ],
     454            valuemap: {
    452455                'id':         'ID',
    453456                'uploadedTo': 'parent'
     
    466469                var args     = {},
    467470                    orderby  = Query.orderby,
    468                     defaults = Query.defaultArgs,
     471                    defaults = Query.defaultProps,
    469472                    query;
    470473
     474                // Remove the `query` property. This isn't linked to a query,
     475                // this *is* the query.
     476                delete props.query;
     477
     478                // Fill default args.
     479                _.defaults( props, defaults );
     480
     481                // Normalize the order.
     482                props.order = props.order.toUpperCase();
     483                if ( 'DESC' !== props.order && 'ASC' !== props.order )
     484                    props.order = defaults.order.toUpperCase();
     485
     486                // Ensure we have a valid orderby value.
     487                if ( ! _.contains( orderby.allowed, props.orderby ) )
     488                    props.orderby = defaults.orderby;
     489
     490                // Generate the query `args` object.
    471491                // Correct any differing property names.
    472492                _.each( props, function( value, prop ) {
     
    474494                });
    475495
    476                 // Fill default args.
    477                 _.defaults( args, defaults );
    478 
    479                 // Normalize the order.
    480                 args.order = args.order.toUpperCase();
    481                 if ( 'DESC' !== args.order && 'ASC' !== args.order )
    482                     args.order = defaults.order.toUpperCase();
    483 
    484                 // Set allowed orderby values.
    485                 // These map directly to attachment keys in most scenarios.
     496                // Fill any other default query args.
     497                _.defaults( args, Query.defaultArgs );
     498
     499                // `props.orderby` does not always map directly to `args.orderby`.
    486500                // Substitute exceptions specified in orderby.keymap.
    487                 args.orderby = orderby.keymap[ args.orderby ] || args.orderby;
    488 
    489                 // Ensure we have a valid orderby value.
    490                 if ( ! _.contains( orderby.allowed, args.orderby ) )
    491                     args.orderby = defaults.orderby;
    492 
    493                 // Search the query cache.
     501                args.orderby = orderby.valuemap[ props.orderby ] || props.orderby;
     502
     503                // Search the query cache for matches.
    494504                query = _.find( queries, function( query ) {
    495505                    return _.isEqual( query.args, args );
     
    498508                // Otherwise, create a new query and add it to the cache.
    499509                if ( ! query ) {
    500                     query = new Query( [], _.extend( options || {}, { args: args } ) );
     510                    query = new Query( [], _.extend( options || {}, {
     511                        props: props,
     512                        args:  args
     513                    } ) );
    501514                    queries.push( query );
    502515                }
Note: See TracChangeset for help on using the changeset viewer.