Add Ember.computed.limit and use it in LimitedArray
arrayComputed was added recently in order to make array computations easier. Using arrayComputed we can improve LimitedArray which now uses an isolated multipurpose Ember.computed.limit.
This commit is contained in:
parent
8e040912ec
commit
36db80d45f
26
assets/scripts/lib/ext/ember/computed.js
Normal file
26
assets/scripts/lib/ext/ember/computed.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
Ember.computed.limit = function(dependentKey, limitKey) {
|
||||
var options = {
|
||||
addedItem: function(array, item, changeMeta, instanceMeta) {
|
||||
var limit = Ember.get(this, limitKey);
|
||||
if (changeMeta.index < limit) {
|
||||
array.insertAt(changeMeta.index, item);
|
||||
if (Ember.get(array, "length") > limit) {
|
||||
array.popObject();
|
||||
}
|
||||
}
|
||||
return array;
|
||||
},
|
||||
removedItem: function(array, item, changeMeta, instanceMeta) {
|
||||
var limit = Ember.get(this, limitKey);
|
||||
if (changeMeta.index < limit && changeMeta.index < Ember.get(array, 'length')) {
|
||||
array.removeAt(changeMeta.index, 1);
|
||||
var toPush = changeMeta.arrayChanged.objectAt(limit);
|
||||
if (toPush) {
|
||||
array.pushObject(toPush);
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
};
|
||||
return Ember.arrayComputed(dependentKey, limitKey, options);
|
||||
};
|
|
@ -1,17 +1,7 @@
|
|||
Travis.LimitedArray = Em.ArrayProxy.extend
|
||||
limit: 10
|
||||
isLoadedBinding: 'content.isLoaded'
|
||||
|
||||
init: ->
|
||||
@_super.apply this, arguments
|
||||
|
||||
arrangedContent: (->
|
||||
content = @get('content')
|
||||
if @get('disabled')
|
||||
content
|
||||
else if content
|
||||
content.slice(0, @get('limit'))
|
||||
).property('content', 'limit', 'disabled')
|
||||
arrangedContent: Ember.computed.limit('content', 'limit')
|
||||
|
||||
totalLength: (->
|
||||
@get('content.length')
|
||||
|
@ -21,54 +11,13 @@ Travis.LimitedArray = Em.ArrayProxy.extend
|
|||
totalLength = @get('totalLength')
|
||||
limit = @get('limit')
|
||||
|
||||
if @get('disabled') || totalLength <= limit
|
||||
0
|
||||
else
|
||||
totalLength - limit
|
||||
).property('totalLength', 'limit', 'disabled')
|
||||
left = totalLength - limit
|
||||
if left < 0 then 0 else left
|
||||
).property('totalLength', 'limit')
|
||||
|
||||
isMore: (->
|
||||
!@get('disabled') && @get('leftLength') > 0
|
||||
@get('leftLength') > 0
|
||||
).property('leftLength')
|
||||
|
||||
showAll: ->
|
||||
@set 'disabled', true
|
||||
|
||||
contentArrayWillChange: (array, index, removedCount, addedCount) ->
|
||||
@_super.apply this, arguments
|
||||
|
||||
return if @get('disabled')
|
||||
|
||||
if removedCount
|
||||
arrangedContent = @get 'arrangedContent'
|
||||
removedObjects = array.slice(index, index + removedCount);
|
||||
arrangedContent.removeObjects(removedObjects)
|
||||
|
||||
contentArrayDidChange: (array, index, removedCount, addedCount) ->
|
||||
@_super.apply this, arguments
|
||||
|
||||
return if @get('disabled')
|
||||
|
||||
limit = @get('limit')
|
||||
|
||||
if addedCount
|
||||
if index < limit
|
||||
addedObjects = array.slice(index, index + addedCount)
|
||||
@get('arrangedContent').replace(index, 0, addedObjects)
|
||||
|
||||
@balanceArray()
|
||||
|
||||
balanceArray: ->
|
||||
limit = @get 'limit'
|
||||
arrangedContent = @get 'arrangedContent'
|
||||
length = arrangedContent.get 'length'
|
||||
content = @get 'content'
|
||||
|
||||
if length > limit
|
||||
arrangedContent.replace(limit, length - limit)
|
||||
else if length < limit && content.get('length') > length
|
||||
count = limit - length
|
||||
while count > 0
|
||||
if next = content.find( (object) -> !arrangedContent.contains(object) )
|
||||
arrangedContent.pushObject(next)
|
||||
count -= 1
|
||||
@set 'limit', Infinity
|
||||
|
|
|
@ -41,7 +41,8 @@ test 'allows to expand array to show all items', ->
|
|||
content = [1, 2, 3]
|
||||
array = Travis.LimitedArray.create content: content, limit: 2
|
||||
|
||||
array.showAll()
|
||||
Ember.run ->
|
||||
array.showAll()
|
||||
|
||||
equal( array.get('length'), 3)
|
||||
deepEqual( array.toArray(), [1, 2, 3])
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
require 'ext/jquery'
|
||||
require 'ext/ember/namespace'
|
||||
require 'ext/ember/computed'
|
||||
require 'app'
|
||||
|
||||
window.ENV ||= {}
|
||||
|
|
Loading…
Reference in New Issue
Block a user