This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/models/model.js.coffee
2013-02-05 14:16:51 -05:00

36 lines
938 B
CoffeeScript

window.Discourse.Model = Ember.Object.extend
# Our own AJAX handler that handles erronous responses
ajax: (url, args) ->
# Error handler
oldError = args.error
args.error = (xhr) =>
oldError($.parseJSON(xhr.responseText).errors)
$.ajax(url, args)
# Update our object from another object
mergeAttributes: (attrs, builders) ->
Object.keys attrs, (k, v) =>
# If they're in a builder we use that
if typeof(v) == 'object' and builders and builder = builders[k]
@set(k, Em.A()) unless @get(k)
col = @get(k)
v.each (obj) -> col.pushObject(builder.create(obj))
else
@set(k, v)
window.Discourse.Model.reopenClass
# Given an array of values, return them in a hash
extractByKey: (collection, klass) ->
retval = {}
return retval unless collection
collection.each (c) ->
obj = klass.create(c)
retval[c.id] = obj
retval