Combined Support Picker

I use the spine and puppet,

and I want to sort my collection and rendering.

but something strange is happening.

'/ api / note / getList' , it returns (and is called when the collection is initialized by view)

[{"id":22,"name":"Test2","isPublic":1},{"id":11,"name":"Test1","isPublic":1},{"id":33,"name":"Test3","isPublic":1}]

and this is my collection,

define [
    'models/Note'
],
(Note) ->
    class NoteCollection extends Backbone.Collection

        model : Note
        url : '/api/note/getList'

        comparator : (item) =>
            console.log item.get 'id'
            return item.get 'id'

and console .log print

22
22
11

type '22' twice? he also does not sort.

How do I do to sort a collection?

[EDIT]

This is my compisteView that initializes the collection

define [    
    'hbs!./noteCollection_tpl'
    './noteItemView'
    'collections/NoteCollection'
],
(noteCollection_tpl, noteItemView, NoteCollection) ->
    class NoteCollectionView extends Backbone.Marionette.CompositeView
        template : noteCollection_tpl
        itemView : noteItemView
        itemViewContainer : '.noteListContainer'
        className : 'noteWrap'

        initialize : (options) ->
            @collection = new NoteCollection() 

@collection = new NoteCollection () => automatically start this run.

+4
source share
1 answer

The problem is that you are using the comparison function as a comparator:

comparator : (item) =>

Backbone " ". :

collection.comparator

[...] sortBy ( , ), ( , ), [...]

sort, :

if (_.isString(this.comparator) || this.comparator.length === 1) {
  this.models = this.sortBy(this.comparator, this);
} else {
  this.models.sort(_.bind(this.comparator, this));
}

, comparator - , (.. comparator.length === 1), sortBy, ; , comparator - , , sort, .

comparator, , . ? , comparator.length . comparator length - , CoffeeScript =>; :

class C
    m1: (x) -> x
    m2: (x) => x

c = new C
console.log(c.m1.length)
console.log(c.m2.length)

1 0 .

: http://jsfiddle.net/ambiguous/GAAap/

JavaScript :

class C
    m: (x) => x

,

__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

=>. return function() { ... } ? , => , a length . , , CoffeeScript.

-> comparator:

comparator: (item) -> item.id

CoffeeScript comparator length, .

: http://jsfiddle.net/ambiguous/WXDcJ/


, epidemian :

https://github.com/jashkenas/coffee-script/pull/2872


. => Backbone.

+10

All Articles