【发布时间】:2013-11-21 17:16:07
【问题描述】:
我正在使用骨干和木偶,
我想对我的集合和渲染视图进行排序。
但是发生了一些奇怪的事情。
'/api/note/getList',返回(视图初始化集合时调用)
[{"id":22,"name":"Test2","isPublic":1},{"id":11,"name":"Test1","isPublic":1},{"id":33,"name":"Test3","isPublic":1}]
这是我的收藏,
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'
console.log 打印
22
22
11
打印'22'两次?它也没有排序。
我应该如何对集合进行排序?
[编辑]
这是我初始化集合的compisteView
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() => 我认为这个运行会自动获取。
【问题讨论】:
-
如果打印出集合,顺序是什么?比较器函数用于对集合进行排序,因此查看打印出来的内容并不一定会给您排序。
-
集合初始化时,调用url。 url加载后如何hook事件?