Last active
August 29, 2015 13:56
-
-
Save spieker/9162307 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = App.Item = App.Model.extend | |
_attrs: ['title', 'note'] | |
token: null | |
title: null | |
note: null | |
max_assignments_count: 0 | |
assignments_count: 0 | |
save: (viddleToken) -> | |
if @get('isNew') | |
@constructor.createRecord(@, | |
"#{App.Model.url}/viddles/#{viddleToken}/items", | |
key: 'item' | |
) | |
else | |
@constructor.updateRecord(@, | |
"#{App.Model.url}/viddles/#{viddleToken}/items/#{@get('token')}", | |
key: 'item' | |
) | |
delete: (viddleToken) -> | |
@constructor.deleteRecord(@, | |
"#{App.Model.url}/viddles/#{viddleToken}/items/#{@get('token')}" | |
) | |
App.Item.reopenClass | |
findAll: (viddleToken) -> | |
App.Model.findAll(App.Item, | |
"#{App.Model.url}/viddles/#{viddleToken}/items" | |
) | |
find: (viddleToken, token) -> | |
App.Model.find(App.Item, | |
"#{App.Model.url}/viddles/#{viddleToken}/items/#{token}", | |
init: | |
token: token | |
) | |
App.Item.primaryKey = 'token' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.Model = Ember.Object.extend | |
isNew: true | |
isSaving: false | |
isLoading: false | |
isDeleting: false | |
isDeleted: false | |
hasError: false | |
_errors: [] | |
updateProperties: (data, options) -> | |
if options.resultKey | |
data = data[options.resultKey] | |
@setProperties(data) | |
@ | |
App.Model.reopenClass | |
identityMap: {} | |
serializeModel: (model, options) -> | |
data = model.getProperties(model.get('_attrs')) | |
if options.key | |
tmp = {} | |
tmp[options.key] = data | |
data = tmp | |
data | |
setIdentityMap: (model) -> | |
type = model.constructor | |
App.Model.identityMap[type.toString()] ||= {} | |
App.Model.identityMap[type.toString()][model.get(type.primaryKey)] = model | |
getIdentityMap: (type, id) -> | |
App.Model.identityMap[type.toString()] ||= {} | |
App.Model.identityMap[type.toString()][id] | |
createRecord: (model, url, options) -> | |
type = model.constructor | |
model.set('isSaving', true) | |
promise = new Promise (resolve, reject) -> | |
$.ajax | |
url: url | |
method: 'POST' | |
dataType: 'json' | |
data: model.constructor.serializeModel(model, options) | |
success: (res, status, xhr) => | |
model.set('isSaving', false) | |
model.updateProperties(res, options) | |
App.Model.setIdentityMap(model) | |
model.set('isNew', false) | |
resolve(model) | |
error: (res, status, xhr) => | |
reject(res) | |
promise | |
updateRecord: (model, url, options) -> | |
model.set('isSaving', true) | |
promise = new Promise (resolve, reject) -> | |
$.ajax | |
url: url | |
method: 'PATCH' | |
dataType: 'json' | |
data: model.constructor.serializeModel(model, options) | |
success: (res, status, xhr) => | |
model.set('isSaving', false) | |
model.updateProperties(model, res) | |
App.Model.setIdentityMap(model) | |
resolve(model) | |
error: (res, status, xhr) => | |
reject(res) | |
promise | |
find: (type, url, options) -> | |
model = App.Model.getIdentityMap(type, options.init[type.primaryKey]) | |
unless model | |
options ||= {} | |
options.init ||= {} | |
model = type.create(options.init) | |
model.set('isNew', false) | |
$.ajax | |
url: url | |
method: 'GET' | |
dataType: 'json' | |
success: (res, status, xhr) => | |
model.set('isLoading', false) | |
model.updateProperties(res, options) | |
App.Model.setIdentityMap(model) | |
error: (res, status, xhr) => | |
reject(res) | |
model | |
findAll: (type, url, options) -> | |
collection = Ember.A() | |
$.ajax | |
url: url | |
method: 'GET' | |
dataType: 'json' | |
success: (res, status, xhr) => | |
$.each res, (index, item) => | |
model = App.Model.getIdentityMap(type, item[type.primaryKey]) | |
unless model | |
model = type.create(item) | |
model.set('isNew', false) | |
App.Model.setIdentityMap(model) | |
collection.pushObject(model) | |
error: (res, status, xhr) => | |
reject(res) | |
collection | |
deleteRecord: (model, url, options) -> | |
model.set('isDeleting', true) | |
promise = new Promise (resolve, reject) -> | |
$.ajax | |
url: url | |
method: 'DELETE' | |
dataType: 'json' | |
success: (res, status, xhr) -> | |
model.set('isDeleting', false) | |
model.set('isDeleted', true) | |
resolve() | |
error: (res, status, xhr) -> | |
reject(res, status, xhr) | |
promise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.Model.url = 'http://api.example.com/' | |
viddle = App.Viddle.create(title: 'test') | |
viddle.save() | |
viddle.set('title', 'something else') | |
viddle.save() | |
viddle2 = App.Viddle.find(viddle.get('token')) | |
viddle == viddle2 # => true (because of identity map) | |
items = App.Item.findAll(viddle.get('token')) | |
item = App.Item.find(viddle.get('token'), 'xxxxxxxx') | |
item.save(viddle.get('token')) | |
item.delete(viddle.get('token')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = App.Viddle = App.Model.extend | |
_attrs: ['title', 'note', 'theme_id'] | |
token: null | |
title: null | |
note: null | |
theme_id: null | |
save: -> | |
if @get('isNew') | |
@constructor.createRecord(@, "#{App.Model.url}/viddles", | |
key: 'viddle' | |
) | |
else | |
@constructor.updateRecord(@, "#{App.Model.url}/viddles/#{@get('token')}", | |
key: 'viddle' | |
) | |
App.Viddle.reopenClass | |
find: (token) -> | |
App.Model.find(App.Viddle, "#{App.Model.url}/viddles/#{token}", | |
init: | |
token: token | |
) | |
App.Viddle.primaryKey = 'token' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment