Skip to content
This repository has been archived by the owner on Jun 5, 2019. It is now read-only.

Introduced FieldData #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions client/source/models/entry.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Model = require 'lib/model'

Bucket = require 'models/bucket'
FieldData = require 'models/field_data'

module.exports = class Entry extends Model
defaults:
Expand All @@ -11,3 +12,13 @@ module.exports = class Entry extends Model
slug: ''
content: {}
urlRoot: '/api/entries'

parse: (response) ->
for key, value of response
continue unless key is 'content'
content = {}
for slug, prop of value
content[slug] = new FieldData prop
response[key] = content
response

22 changes: 22 additions & 0 deletions client/source/models/field_data.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Model = require 'lib/model'

module.exports = class FieldData extends Model
defaults:
data: value: null

getData: ->
@data

getValue: (path) ->
path = 'value' unless path
@get('data')[path]

setValue: (path, value) ->
unless value
value = path
path = 'value'

@get('data')[path] = value
# TODO should trigger some event, not sure about Chaplin
#@trigger 'change', {path: path, value: value}
@
11 changes: 6 additions & 5 deletions client/source/views/entries/edit.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Model = require 'lib/model'
PageView = require 'views/base/page'
FormMixin = require 'views/base/mixins/form'
FieldTypeInputView = require 'views/fields/input'
FieldData = require 'models/field_data'
Chaplin = require 'chaplin'

tpl = require 'templates/entries/edit'
Expand Down Expand Up @@ -59,7 +60,7 @@ module.exports = class EntryEditView extends PageView
content = @model.get('content')

_.each @bucket.get('fields'), (field) =>
fieldValue = content[field.slug]
fieldValue = content[field.slug]?.getValue()
fieldModel = new Model _.extend field, value: fieldValue

@subview 'field_'+field.slug, new FieldTypeInputView
Expand Down Expand Up @@ -118,15 +119,15 @@ module.exports = class EntryEditView extends PageView

content = {}
for field in @bucket.get('fields')
content[field.slug] = @subview("field_#{field.slug}").getValue?()
continue if content[field.slug]
content[field.slug] = new FieldData fieldType: field.fieldType
content[field.slug].setValue @subview("field_#{field.slug}").getValue?()
continue if content[field.slug].getValue()

data = @subview "field_#{field.slug}"
.$el.formParams no
simpleValue = data[field.slug]

content[field.slug] = if simpleValue? then simpleValue else data

content[field.slug].setValue if simpleValue? then simpleValue else data
@model.set content: content

status = @model.get 'status'
Expand Down
9 changes: 8 additions & 1 deletion server/lib/renderer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ module.exports = (hbs) ->
for entry in entries

# Make content attributes first-level tags, ie. `{{body}}` instead of `{{content.body}}`
entryJSON = _.extend entry, entry.content
# `{{body.data.value}}` becomes `{{body}}` while `{{body.data.html}}` becomes `{{body.html}}`
content = {}
for slug, fieldData of entry.content
for key, value of fieldData.data
newKey = if key is 'value' then slug else "#{slug}_#{key}"
content[newKey] = value

entryJSON = _.extend entry, content
delete entryJSON.content

try
Expand Down
8 changes: 8 additions & 0 deletions server/models/entry.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ chrono.parsers.NowParser = (text, ref, opt) ->

parser

fieldDataSchema = new mongoose.Schema
fieldType:
type: String
required: yes
data:
type: Object
default: value: null

entrySchema = new mongoose.Schema
title:
type: String
Expand Down