このバックボーン コレクションを考えると
define [
'underscore',
'backbone',
'cs!models/floor'
], ( _, Backbone, Floor ) ->
return Backbone.Collection.extend
model: Floor
url: ->
return '/api/hotels/' + @hotelId + '/floors'
initialize: (models, options) ->
if ( options.hotelId )
@hotelId = options.hotelId
@.fetch()
parse: (response) ->
response.floors
alreadyExist: ->
@.filter( (floor) ->
return floor.get('number') == @.attrs.get('number')
)
以下の方法でビューから新しいモデルを追加すると、モデルがコレクション内に既に存在するかどうかを検証するにはどうすればよいですか?
add_floor: (e) ->
console.log ' Saving Floor '
e.preventDefault()
floorNumber = $('input[name=floorNumber]').val()
floorDescription = $('input[name=floorDescription]').val()
return new NoticeView({ message: "Please enter a Floor Number.", displayLength: 10000 }) unless floorNumber
if ! @collection.add({ number: floorNumber}).alreadyExist()
@collection.create({ number: floorNumber, description: floorDescription }, {
error: (model, response) ->
# $(e.target).removeClass('waiting');
new ErrorView({ message: "Problem saving Floor " + response.responseText, displayLength: 10000 })
success : (model, response) ->
console.log model
console.log response
new NoticeView({ message: "Floor successfully saved.", displayLength: 10000 })
})
else
new ErrorView({ message: "Floor already exist." + response.responseText, displayLength: 10000 })