2011-12-19 13:15:11 +00:00
|
|
|
class Locomotive.Models.CustomField extends Backbone.Model
|
|
|
|
|
|
|
|
initialize: ->
|
2011-12-22 01:59:30 +00:00
|
|
|
@_normalize()
|
|
|
|
|
2011-12-19 13:15:11 +00:00
|
|
|
unless @get('name')?
|
|
|
|
@set name: @get('label').slugify()
|
|
|
|
|
2011-12-22 01:59:30 +00:00
|
|
|
_normalize: ->
|
|
|
|
@set
|
|
|
|
select_options: new Locomotive.Models.CustomFieldSelectOptionsCollection(@get('select_options'))
|
|
|
|
|
2012-02-01 01:01:42 +00:00
|
|
|
_undesired_fields:
|
|
|
|
['select_options', 'type_text', 'text_formatting_text', 'inverse_of_text', 'class_name_text', 'undefined_text', 'undefined', 'created_at', 'updated_at']
|
|
|
|
|
|
|
|
_relationship_fields:
|
|
|
|
['class_name', 'inverse_of', 'ui_enabled']
|
|
|
|
|
|
|
|
is_relationship_type: ->
|
|
|
|
_.include(['belongs_to', 'has_many'], @get('type'))
|
|
|
|
|
2011-12-19 13:15:11 +00:00
|
|
|
toJSONForSave: ->
|
|
|
|
_.tap {}, (hash) =>
|
|
|
|
for key, value of @.toJSON()
|
2012-02-01 01:01:42 +00:00
|
|
|
unless _.include(@_undesired_fields, key)
|
|
|
|
if _.include(@_relationship_fields, key)
|
|
|
|
hash[key] = value if @is_relationship_type()
|
|
|
|
else
|
|
|
|
hash[key] = value
|
|
|
|
|
2012-01-14 00:38:09 +00:00
|
|
|
hash.select_options_attributes = @get('select_options').toJSONForSave() if @get('select_options')? && @get('select_options').length > 0
|
2011-12-19 13:15:11 +00:00
|
|
|
|
|
|
|
class Locomotive.Models.CustomFieldsCollection extends Backbone.Collection
|
|
|
|
|
|
|
|
model: Locomotive.Models.CustomField
|
|
|
|
|
|
|
|
toJSONForSave: ->
|
|
|
|
@map (model) => model.toJSONForSave()
|