-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray-controller.js.coffee
More file actions
executable file
·68 lines (53 loc) · 2.03 KB
/
array-controller.js.coffee
File metadata and controls
executable file
·68 lines (53 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
_ = require "underscore"
Backbone = require "backbone"
class @ArrayController
constructor: (objects, options) ->
@array = []
@length = 0
@add objects, options if objects
add: (objects, options = {}) ->
objects = if _.isArray(objects) then objects.slice() else [objects]
@length += objects.length
Array::splice.apply(@array, [@array.length, 0].concat(objects))
@sort({silent: true})
if !options.silent
for object in objects
@trigger('add', object, this, options)
remove: (objects, options = {}) ->
objects = if _.isArray(objects) then objects.slice() else [objects]
for object in objects
index = @indexOf(object)
if index >= 0
@length -= 1
@array.splice(index, 1)
@trigger('remove', object, this, options) if !options.silent
reset: (objects = [], options = {}) ->
@array = []
@length = 0
@add(objects, _.extend({silent: true}, options))
@trigger('reset', this, options) if !options.silent
at: (index) ->
return @array[index]
comparator: (a, b) ->
a.toLowerCase() > b.toLowerCase()
sort: (options = {}) ->
boundComparator = _.bind(@comparator, this)
if @comparator.length == 1
@array = @sortBy(boundComparator);
else
@array.sort(boundComparator);
@trigger('reset', this, options) if !options.silent
this
_.extend(@prototype, Backbone.Events)
methods = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find',
'detect', 'filter', 'select', 'reject', 'every', 'all', 'some', 'any',
'include', 'contains', 'invoke', 'max', 'min', 'sortBy', 'sortedIndex',
'toArray', 'size', 'first', 'initial', 'rest', 'last', 'without', 'indexOf',
'shuffle', 'lastIndexOf', 'isEmpty', 'groupBy'];
# Mix in each Underscore method as a proxy to `ArrayController#array`.
_.each(methods, (method) =>
@::[method] = ->
return _[method].apply(_, [@array].concat(_.toArray(arguments)));
)
if typeof module != 'undefined' && module.exports
module.exports = @ArrayController