Add config.pushAtKeyPath/removeAtKeyPath

This commit is contained in:
Kevin Sawicki & Nathan Sobo
2013-05-16 10:51:23 -07:00
parent 2b49a04227
commit 3b0748fe44
6 changed files with 52 additions and 7 deletions

View File

@@ -144,6 +144,30 @@ class Config
@update()
value
# Push the value to the array at the key path.
#
# keyPath - The {String} key path.
# value - The value to push to the array.
#
# Returns the new array length of the setting.
pushAtKeyPath: (keyPath, value) ->
arrayValue = @get(keyPath) ? []
result = arrayValue.push(value)
@set(keyPath, arrayValue)
result
# Remove the value from the array at the key path.
#
# keyPath - The {String} key path.
# value - The value to remove from the array.
#
# Returns the new array value of the setting.
removeAtKeyPath: (keyPath, value) ->
arrayValue = @get(keyPath) ? []
result = _.remove(arrayValue, value)
@set(keyPath, arrayValue)
result
# Establishes an event listener for a given key.
#
# `callback` is fired immediately and whenever the value of the key is changed

View File

@@ -29,12 +29,10 @@ class PackageConfigPanel extends ConfigPanel
@on 'change', '#packages input[type=checkbox]', (e) ->
checkbox = $(e.target)
name = checkbox.closest('tr').attr('name')
disabledPackages = config.get('core.disabledPackages')
if checkbox.attr('checked')
_.remove(disabledPackages, name)
config.removeAtKeyPath('core.disabledPackages', name)
else
disabledPackages.push(name)
config.set('core.disabledPackages', disabledPackages)
config.pushAtKeyPath('core.disabledPackages', name)
@observeConfig 'core.disabledPackages', (disabledPackages) =>
@packageTableBody.find("input[type='checkbox']").attr('checked', true)

View File

@@ -4,6 +4,7 @@ _.mixin
remove: (array, element) ->
index = array.indexOf(element)
array.splice(index, 1) if index >= 0
array
spliceWithArray: (originalArray, start, length, insertedArray, chunkSize=100000) ->
if insertedArray.length < chunkSize