Refactored the item equips view to limit items by slot and also moved the item name loading to the node class itself.

This commit is contained in:
Scott Erickson 2014-08-28 11:24:45 -07:00
parent 94065a0c4c
commit 8b056cfb8f
6 changed files with 95 additions and 17 deletions

View file

@ -4,10 +4,14 @@ module.exports = class CocoCollection extends Backbone.Collection
loaded: false
model: null
initialize: ->
initialize: (models, options) ->
options ?= {}
@model ?= options.model
if not @model
console.error @constructor.name, 'does not have a model defined. This will not do!'
super()
super(models, options)
@setProjection options.project
if options.url then @url = options.url
@once 'sync', =>
@loaded = true
model.loaded = true for model in @models
@ -15,7 +19,13 @@ module.exports = class CocoCollection extends Backbone.Collection
getURL: ->
return if _.isString @url then @url else @url()
fetch: ->
@jqxhr = super(arguments...)
fetch: (options) ->
options ?= {}
if @project
options.data ?= {}
options.data.project = @project.join(',')
@jqxhr = super(options)
@loading = true
@jqxhr
setProjection: (@project) ->

View file

@ -24,8 +24,5 @@
else
strong(data-i18n="loading_error.unknown") Unknown error.
if resourceIndex !== undefined
button.btn.btn-xs.retry-loading-resource(data-i18n="common.retry", data-resource-index=resourceIndex) Retry
if requestIndex !== undefined
button.btn.btn-xs.retry-loading-request(data-i18n="common.retry", data-request-index=requestIndex) Retry
button.btn.btn-xs.retry-loading-resource(data-i18n="common.retry", data-resource-index=resourceIndex) Retry
button.btn.btn-xs.skip-loading-resource(data-i18n="common.skip", data-resource-index=resourceIndex) Skip

View file

@ -33,8 +33,6 @@ module.exports = class ThangComponentsEditView extends CocoView
@world = options.world
@level = options.level
@loadComponents(@components)
# Need to grab the ThangTypes so that we can autocomplete items in inventory based on them.
@itemThangTypes = @supermodel.loadCollection(new ItemThangTypeSearchCollection(), 'thangs').model
setThangType: (@thangType) ->
return unless componentRefs = @thangType?.get('components')

View file

@ -1,5 +1,7 @@
WorldSelectModal = require './modals/WorldSelectModal'
ThangType = require '/models/ThangType'
LevelComponent = require 'models/LevelComponent'
CocoCollection = require 'collections/CocoCollection'
makeButton = -> $('<a class="btn btn-primary btn-xs treema-map-button"><span class="glyphicon glyphicon-screenshot"></span></a>')
shorten = (f) -> parseFloat(f.toFixed(1))
@ -221,11 +223,63 @@ module.exports.ThangTypeNode = class ThangTypeNode extends TreemaNode.nodeMap.st
else
@data = null
module.exports.ItemThangTypeNode = class ThangTypeNode extends ThangTypeNode
module.exports.ItemThangTypeNode = ItemThangTypeNode = class ItemThangTypeNode extends TreemaNode.nodeMap.string
valueClass: 'treema-item-thang-type'
@items: null
@itemsCollection: null
constructor: ->
super(arguments...)
@getItems()
unless ItemThangTypeNode.itemsCollection.loaded
ItemThangTypeNode.itemsCollection.once('sync', @refreshDisplay, @)
buildValueForDisplay: (valEl, data) ->
super(valEl, data)
thangTypeNames = (m.get('name') for m in @settings.supermodel.getModels ThangType when m.get('kind') is 'Item')
input = valEl.find('input').autocomplete(source: thangTypeNames, minLength: 0, delay: 0, autoFocus: true)
input.val(@thangType?.get('name') or 'None')
@buildValueForDisplaySimply(valEl, @getCurrentItem() or '')
valEl
buildValueForEditing: (valEl, data) ->
super(valEl, data)
if ItemThangTypeNode.items
source = (item.name for item in ItemThangTypeNode.items when @keyForParent in item.slots)
input = valEl.find('input').autocomplete(source: source, minLength: 0, delay: 0, autoFocus: true)
input.val(@getCurrentItem() or '')
valEl
getCurrentItem: ->
window.itemData = @getData()
window.items = ItemThangTypeNode.items
return null unless ItemThangTypeNode.items
original = @getData()
return null unless original
item = _.find ItemThangTypeNode.items, { original: original }
item?.name or '...'
getItems: ->
return if ItemThangTypeNode.itemsCollection
ItemThangTypeNode.itemsCollection = new CocoCollection([], {
url: '/db/thang.type'
project:['name', 'components', 'original']
model: ThangType
})
res = ItemThangTypeNode.itemsCollection.fetch()
ItemThangTypeNode.itemsCollection.once 'sync', => @processItems(ItemThangTypeNode.itemsCollection)
processItems: (itemCollection) ->
ItemThangTypeNode.items = []
for itemThang in itemCollection.models
itemComponent = _.find itemThang.get('components'), {original: LevelComponent.ItemID}
continue unless itemComponent
slots = itemComponent.config?.slots
continue unless slots?.length
ItemThangTypeNode.items.push {
name: itemThang.get('name')
original: itemThang.get('original')
slots: slots
}
saveChanges: ->
thangTypeName = @$el.find('input').val()
item = _.find(ItemThangTypeNode.items, {name: thangTypeName})
return @remove() unless item
@data = item.original

View file

@ -17,7 +17,7 @@ module.exports = class CocoView extends Backbone.View
events:
'click .retry-loading-resource': 'onRetryResource'
'click .retry-loading-request': 'onRetryRequest'
'click .skip-loading-resource': 'onSkipResource'
subscriptions: {}
shortcuts: {}
@ -148,6 +148,13 @@ module.exports = class CocoView extends Backbone.View
res.load()
@$el.find('.progress').show()
$(e.target).closest('.loading-error-alert').remove()
onSkipResource: (e) ->
res = @supermodel.getResource($(e.target).data('resource-index'))
return unless res and res.isFailed
res.markLoaded()
@$el.find('.progress').show()
$(e.target).closest('.loading-error-alert').remove()
# Modals

View file

@ -0,0 +1,12 @@
CocoCollection = require 'collections/CocoCollection'
LevelComponent = require 'models/LevelComponent'
describe 'CocoCollection', ->
it 'can be given a project function to include a project query arg', ->
collection = new CocoCollection([], {
url: '/db/level.component'
project:['name', 'description']
model: LevelComponent
})
collection.fetch({data: {view: 'items'}})
expect(jasmine.Ajax.requests.mostRecent().url).toBe('/db/level.component?view=items&project=name%2Cdescription')