Achievement queries are now strings server sided but remain objects for the client

This commit is contained in:
Ruben Vereecken 2014-05-21 19:47:17 +02:00
parent c26f96f33e
commit 287019242f
3 changed files with 57 additions and 31 deletions
app
schemas/models
views/editor/achievement
server/achievements

View file

@ -20,42 +20,37 @@ MongoFindQuerySchema =
id: 'mongoFindQuery'
type: 'object'
patternProperties:
'^[-a-zA-Z0-9_]*$':
#'^[-a-zA-Z0-9_]*$':
'^[-a-zA-Z0-9]*$':
oneOf: [
#{ $ref: '#/definitions/' + MongoQueryOperatorSchema.id},
{ type: 'string' }
]
properties:
'levelID':
oneOf: [
{ type: 'string' }
]
additionalProperties: false
definitions: {}
MongoFindQuerySchema.definitions[MongoQueryOperatorSchema.id] = MongoQueryOperatorSchema
AchievementSchema =
type: 'object'
properties:
name: c.shortString({title: 'Display Name'})
query:
#type: 'object'
$ref: '#/definitions/' + MongoFindQuerySchema.id
worth: { type: 'number' }
collection: { type: 'string' }
description: { type: 'string' }
userField: { type: 'string' }
related: c.objectId
proportionalTo:
type: 'string'
description: 'For repeatables only. Denotes the field a repeatable achievement needs for its calculations'
definitions: {}
AchievementSchema.definitions[MongoFindQuerySchema.id] = MongoFindQuerySchema
AchievementSchema = c.object()
c.extendNamedProperties AchievementSchema
c.extendBasicProperties AchievementSchema, 'article'
c.extendSearchableProperties AchievementSchema
_.extend(AchievementSchema.properties,
query:
type:'object'
#$ref: '#/definitions/' + MongoFindQuerySchema.id
worth: { type: 'number' }
collection: { type: 'string' }
description: { type: 'string' }
userField: { type: 'string' }
related: c.objectId
proportionalTo:
type: 'string'
description: 'For repeatables only. Denotes the field a repeatable achievement needs for its calculations'
)
AchievementSchema.definitions = {}
AchievementSchema.definitions[MongoFindQuerySchema.id] = MongoFindQuerySchema
module.exports = AchievementSchema

View file

@ -9,10 +9,10 @@ module.exports = class AchievementEditView extends View
startsLoading: true
events:
'click #save-button': 'openSaveModal'
'click #save-button': 'saveAchievement'
subscriptions:
'save-achievement': 'saveAchievement'
'save-new': 'saveAchievement'
constructor: (options, @achievementID) ->
super options
@ -49,7 +49,7 @@ module.exports = class AchievementEditView extends View
@treema.build()
pushChangesToPreview: =>
'TODO'
'TODO' # TODO might want some intrinsic preview thing
getRenderData: (context={}) ->
context = super(context)
@ -58,7 +58,18 @@ module.exports = class AchievementEditView extends View
context
openSaveModal: ->
'TODO'
'Maybe later' # TODO
saveAchievement: (e) ->
'TODO'
@treema.endExistingEdits()
for key, value of @treema.data
@achievement.set(key, value)
res = @achievement.save()
res.error =>
console.log 'Failed to save achievement'
res.success =>
url = "/editor/achievement/#{@achievement.get('slug') or @achievement.id}"
document.location.href = url

View file

@ -1,6 +1,7 @@
mongoose = require('mongoose')
plugins = require('../plugins/plugins')
jsonschema = require('../../app/schemas/models/achievement')
log = require 'winston'
# `pre` and `post` are not called for update operations executed directly on the database,
# including `Model.update`,`.findByIdAndUpdate`,`.findOneAndUpdate`, `.findOneAndRemove`,and `.findByIdAndRemove`.order
@ -8,9 +9,28 @@ jsonschema = require('../../app/schemas/models/achievement')
# or `remove` functions on the document. See [explanation](http://github.com/LearnBoost/mongoose/issues/964).
AchievementSchema = new mongoose.Schema({
query: Object
userField: String
}, {strict: false})
AchievementSchema.methods.objectifyQuery = () ->
try
@set('query', JSON.parse(@get('query'))) if typeof @get('query') == "string"
catch error
#log.error "Couldn't convert query string to object because of #{error}"
@set('query', {})
console.log @
AchievementSchema.methods.stringifyQuery = () ->
@set('query', JSON.stringify(@get('query'))) if typeof @get('query') != "string"
console.log @
AchievementSchema.post('init', (doc) -> doc.objectifyQuery())
AchievementSchema.pre('save', (next) ->
@stringifyQuery()
next()
)
AchievementSchema.plugin(plugins.SearchablePlugin, {searchable: ['name']})
AchievementSchema.plugin(plugins.NamedPlugin)