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
Sam fa8a84f20c removed sugar.js, port functionality to moment and underscore.js
bring in latest ace from local so we don't mess up with https
2013-06-11 15:27:26 +10:00

60 lines
1.4 KiB
JavaScript

/**
A base object we can use to handle models in the Discourse client application.
@class Model
@extends Ember.Object
@uses Discourse.Presence
@namespace Discourse
@module Discourse
**/
Discourse.Model = Ember.Object.extend(Discourse.Presence, {
/**
Update our object from another object
@method mergeAttributes
@param {Object} attrs The attributes we want to merge with
@param {Object} builders Optional builders to use when merging attributes
**/
mergeAttributes: function(attrs, builders) {
var _this = this;
_.each(attrs, function(v,k) {
// If they're in a builder we use that
var builder, col;
if (typeof v === 'object' && builders && (builder = builders[k])) {
if (!_this.get(k)) {
_this.set(k, Em.A());
}
col = _this.get(k);
_.each(v,function(obj) {
col.pushObject(builder.create(obj));
});
} else {
_this.set(k, v);
}
});
}
});
Discourse.Model.reopenClass({
/**
Given an array of values, return them in a hash
@method extractByKey
@param {Object} collection The collection of values
@param {Object} klass Optional The class to instantiate
**/
extractByKey: function(collection, klass) {
var retval = {};
if (!collection) return retval;
_.each(collection,function(c) {
retval[c.id] = klass.create(c);
});
return retval;
}
});