在Backbone中对集合进行排序

时间:2013-09-30 22:57:42

标签: javascript jquery ajax json backbone.js

好的,我有一个返回播客的JSON提要的API。

这些播客出现在前端,但顺序与我需要的顺序完全相反。我更加困惑,因为它调用的JSON提供符合我想要的正确顺序,但我不能让它像前端那样工作。请看一下我正在使用的代码:

http://pastebin.com/bSamtFUw

var cache = new CacheProvider;


var Photo = Backbone.Model.extend({
   subalbum: function() { return 'c' + gallery._currentsub; }
});



var PhotoCollection = Backbone.Collection.extend({
    model: Photo,
    comparator: function(item) {
        return item.get('pid');
    }
});

function removeFallbacks(){
  var query = $('.jstest,.gallery');
        if(query.length){
          query.remove();
        }
}


var IndexView = Backbone.View.extend({
    el: $('#main'),
    indexTemplate: $("#indexTmpl").template(),

    render: function() {
        removeFallbacks();
        var sg = this;

        this.el.fadeOut('fast', function() {
        sg.el.empty();
        $.tmpl(sg.indexTemplate, sg.model.toArray()).appendTo(sg.el);
        sg.el.fadeIn('fast');
        });
        return this;
    }

});


var SubalbumView = Backbone.View.extend({
    el: $('#main'),
    indexTemplate: $("#subindexTmpl").template(),

    initialize: function(options){

    },

    render: function() {
        var sg = this;
        removeFallbacks();
        this.el.fadeOut('fast', function() {
            sg.el.empty();
            $.tmpl(sg.indexTemplate, sg.model.toArray()).appendTo(sg.el);
            sg.el.fadeIn('fast');
        });
        return this;
    }

});


var PhotoView = Backbone.View.extend({
    el: $('#main'),
    itemTemplate: $("#itemTmpl").template(),


    initialize: function(options) {
        this.album = options.album;

    },

    render: function() {
        var sg = this;
        removeFallbacks();     
        this.el.fadeOut('fast', function() {
            sg.el.empty();
            $.tmpl(sg.itemTemplate, sg.model).appendTo(sg.el);
            sg.el.fadeIn('fast');
        });
        return this;
    }
});




var Gallery = Backbone.Controller.extend({
    _index: null,
    _photos: null,
    _album :null,
    _subalbums:null,
    _subphotos:null,
    _data:null,
    _photosview:null,
    _currentsub:null,

    routes: {
        "": "index",
        "subalbum/:id": "hashsub",
        "subalbum/:id/" : "directphoto",
        "subalbum/:id/:num" : "hashphoto"
    },

    initialize: function(options) {

        var ws = this;

        if (this._index === null){
            $.ajax({
                url: 'http://mikegradio.com/api/v1/podcasts/export/?tags__title=sports&sort_by=-rank&limit=50',
                dataType: 'json',
                data: {},
                success: function(data) {
                    ws._data = data;
                    ws._photos = new PhotoCollection(data);
                    ws._index = new IndexView({model: ws._photos}); 
                    Backbone.history.loadUrl();

                }
            });
            return this;
        }
        return this;
    },


    index: function() {
        this._index.render();
    },


    hashsub:function(id){

       var properindex = id.replace('c','');    
       this._currentsub = properindex;
       this._subphotos = cache.get('pc' + properindex) || cache.set('pc' + properindex, new PhotoCollection(this._data[properindex].subalbum));
       this._subalbums = cache.get('sv' + properindex) || cache.set('sv' + properindex, new SubalbumView({model: this._subphotos}));
       this._subalbums.render();


    },

    directphoto: function(id){

    },

      hashphoto: function(num, id){
        this._currentsub = num;

        num = num.replace('c','');

        if(this._subphotos == undefined){
           this._subphotos = cache.get('pc' + num) || cache.set('pc' + num, new PhotoCollection(this._data[num].subalbum));
         }  
        this._subphotos.at(id)._view = new PhotoView({model: this._subphotos.at(id)});
        this._subphotos.at(id)._view.render();

      }
});


gallery = new Gallery();
Backbone.history.start();

http://pastebin.com/VZ6aTj9T#

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <link rel="stylesheet" href="gallery.css" type="text/css" media="screen" charset="utf-8" />
        <link rel="stylesheet" href="shadows.css" type="text/css" media="screen" charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link rel="stylesheet" href="buttons.css" type="text/css" media="screen" charset="utf-8" />
        <link rel="stylesheet" href="ipad.css" type="text/css" media="screen" charset="utf-8" />
        <link rel="stylesheet" href="desktop.css" type="text/css" media="screen" charset="utf-8" />

                <script id="indexTmpl" type="text/x-jquery-tmpl">
                <div class="item drop-shadow round">
                 <div class="item-image">
                     <a href="#subalbum/${cid}"><img src="${attributes.image}" alt="${attributes.title}" />
                     </a>
                 </div>

                </div>
                </script>


                <script id="subindexTmpl" type="text/x-jquery-tmpl">
                <div class="track drop-shadow round">
                 <div class="item-image subalbum">
                      <div class="item-artist">${attributes.title}</div>
                   <audio controls preload="none" id="audioControl" id="audio-player" src="${attributes.media}" type="audio/mp3" controls="controls"></audio>
                      <div class="item-price">${attributes.duration}</div>
                      <a href="#subalbum/${subalbum}/${attributes.cid}">Shop</a>
                </div>

                 </div>
                </script>

                    <script id="itemTmpl" type="text/x-jquery-tmpl">
                <div class="item-detail">
                  <div class="item-image drop-shadow round"><img src="${attributes.large_image}" alt="${attributes.title}" /></div>
                  <div class="item-info">
                    <div class="item-artist">${attributes.artist}</div>
                    <div class="item-title">${attributes.title}</div>
                    <div class="item-price">$${attributes.price}</div>
                    <br />
                    <div class="item-link"><a href="${attributes.url}" class="button">Buy this item</a></div>
                    <div class="back-link"><a href="#" class="button">&laquo; Back to Albums</a></div>
                  </div>
                </div>
                </script>

    </head>
    <body>
        <div id="container">
            <div id="main">
            </div>
        </div>
        <script src="LAB.min.js" type="text/javascript"></script>


<script type="text/javascript">
           $LAB
           .script("jquery-1.4.4.min.js").wait()
           .script("jquery.tmpl.min.js")
           .script("underscore-min.js")
           .script("backbone-min.js")
           .script("cacheprovider.js").wait()
           .script("gallery.js");      
        </script>
    </body>
</html>

http://mikegradio.com/api/v1/podcasts/export/?tags__title=sports&sort_by=-rank&limit=50

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您是否尝试对集合进行反向排序:(注意否定以使集合反转?)

comparator: function(item) {
    return -item.get('pid');
}