mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-03-14 07:00:01 -04:00
Built a new 'patch' method for CocoModel, which saves only changed values to the server.
This commit is contained in:
parent
69fc378930
commit
389d530eaa
3 changed files with 90 additions and 1 deletions
|
@ -97,6 +97,18 @@ class CocoModel extends Backbone.Model
|
|||
noty text: "#{errorMessage}: #{res.status} #{res.statusText}", layout: 'topCenter', type: 'error', killer: false, timeout: 10000
|
||||
@trigger "save", @
|
||||
return super attrs, options
|
||||
|
||||
patch: (options) ->
|
||||
return false unless @_revertAttributes
|
||||
options ?= {}
|
||||
options.patch = true
|
||||
|
||||
attrs = {_id: @id}
|
||||
for key in _.keys @attributes
|
||||
unless _.isEqual @attributes[key], @_revertAttributes[key]
|
||||
attrs[key] = @attributes[key]
|
||||
|
||||
@save(attrs, options)
|
||||
|
||||
fetch: ->
|
||||
@jqxhr = super(arguments...)
|
||||
|
|
|
@ -113,7 +113,7 @@ module.exports = class SettingsView extends View
|
|||
|
||||
return unless me.hasLocalChanges()
|
||||
|
||||
res = me.save()
|
||||
res = me.patch()
|
||||
return unless res
|
||||
save = $('#save-button', @$el).text($.i18n.t('common.saving', defaultValue: 'Saving...'))
|
||||
.removeClass('btn-danger').addClass('btn-success').show()
|
||||
|
|
77
test/app/models/CocoModel.spec.coffee
Normal file
77
test/app/models/CocoModel.spec.coffee
Normal file
|
@ -0,0 +1,77 @@
|
|||
CocoModel = require 'models/CocoModel'
|
||||
|
||||
class BlandClass extends CocoModel
|
||||
@className: 'Bland'
|
||||
@schema: {
|
||||
type: 'object'
|
||||
additionalProperties: false
|
||||
properties:
|
||||
number: {type: 'number'}
|
||||
object: {type: 'object'}
|
||||
string: {type: 'string'}
|
||||
_id: {type: 'string'}
|
||||
}
|
||||
urlRoot: '/db/bland'
|
||||
|
||||
describe 'CocoModel', ->
|
||||
describe 'save', ->
|
||||
|
||||
it 'saves to db/<urlRoot>', ->
|
||||
b = new BlandClass({})
|
||||
res = b.save()
|
||||
request = jasmine.Ajax.requests.mostRecent()
|
||||
expect(res).toBeDefined()
|
||||
expect(request.url).toBe(b.urlRoot)
|
||||
expect(request.method).toBe('POST')
|
||||
|
||||
it 'does not save if the data is invalid based on the schema', ->
|
||||
b = new BlandClass({number: 'NaN'})
|
||||
res = b.save()
|
||||
expect(res).toBe(false)
|
||||
request = jasmine.Ajax.requests.mostRecent()
|
||||
expect(request).toBeUndefined()
|
||||
|
||||
it 'uses PUT when _id is included', ->
|
||||
b = new BlandClass({_id: 'test'})
|
||||
b.save()
|
||||
request = jasmine.Ajax.requests.mostRecent()
|
||||
expect(request.method).toBe('PUT')
|
||||
|
||||
describe 'patch', ->
|
||||
it 'PATCHes only properties that have changed', ->
|
||||
b = new BlandClass({_id: 'test', number:1})
|
||||
b.loaded = true
|
||||
b.set('string', 'string')
|
||||
b.patch()
|
||||
request = jasmine.Ajax.requests.mostRecent()
|
||||
params = JSON.parse request.params
|
||||
expect(params.string).toBeDefined()
|
||||
expect(params.number).toBeUndefined()
|
||||
|
||||
it 'collates all changes made over several sets', ->
|
||||
b = new BlandClass({_id: 'test', number:1})
|
||||
b.loaded = true
|
||||
b.set('string', 'string')
|
||||
b.set('object', {4:5})
|
||||
b.patch()
|
||||
request = jasmine.Ajax.requests.mostRecent()
|
||||
params = JSON.parse request.params
|
||||
expect(params.string).toBeDefined()
|
||||
expect(params.object).toBeDefined()
|
||||
expect(params.number).toBeUndefined()
|
||||
|
||||
it 'does not include data from previous patches', ->
|
||||
b = new BlandClass({_id: 'test', number:1})
|
||||
b.loaded = true
|
||||
b.set('object', {1:2})
|
||||
b.patch()
|
||||
request = jasmine.Ajax.requests.mostRecent()
|
||||
attrs = JSON.stringify(b.attributes) # server responds with all
|
||||
request.response({status: 200, responseText: attrs})
|
||||
|
||||
b.set('number', 3)
|
||||
b.patch()
|
||||
request = jasmine.Ajax.requests.mostRecent()
|
||||
params = JSON.parse request.params
|
||||
expect(params.object).toBeUndefined()
|
||||
|
Loading…
Reference in a new issue