Skip to content

Instantly share code, notes, and snippets.

@thomaslang
Created September 1, 2009 14:59
Show Gist options
  • Save thomaslang/179138 to your computer and use it in GitHub Desktop.
Save thomaslang/179138 to your computer and use it in GitHub Desktop.
// ==========================================================================
// Project: BB.Concept
// Copyright: ©2009 My Company, Inc.
// ==========================================================================
/*globals BB */
/** @class
(Document your Model here)
@extends SC.Record
@version 0.1
*/
BB.Concept = SC.Record.extend(
/** @scope BB.Concept.prototype */ {
couchView: "concepts",
primaryKey: "_id",
user: SC.Record.toOne('BB.User', { isRequired: YES }),
library: SC.Record.toOne('BB.Library', { isRequired: YES }),
date: SC.Record.attr( 'SC.DateTime', { isRequired: YES }),
toUserLibrary: SC.Record.attr(Boolean, {defaultValue: NO}),
descriptions: SC.Record.toManyByKey('BB.Description', 'concept', {orderBy: "date DESC"}),
units: SC.Record.toManyByKey('BB.Unit', 'concept', {orderBy: "date"}),
//unitsStoreKeys: [],
//unitsStoreKeysBinding: '.units.storeKeys',
properties: SC.Record.toManyByKey('BB.Property', 'concept', {orderBy: "date"}),
relations: SC.RecordAttribute.create(
/** @scope SC.ToManyRelation.prototype */ {
/** @private - adapted for this toMany relationship */
toType: function(record, key, value) {
var conditions = 'concept1 = {record} OR concept2 = {record}';
var parameters = {record: record};
var orderBy = "date";
var recordType = BB.Relation;
var store = record.get('store');
var queryCacheKey = '__many_query__'+SC.guidFor(recordType)+"__"+conditions;
var query = record[queryCacheKey];
var recsCacheKey = '__many_records__'+SC.guidFor(recordType)+"__"+conditions;
var recs = record[recsCacheKey] ;
if (!query) {
query = record[queryCacheKey] = SC.Query.create({
recordType: recordType,
conditions: conditions,
parameters: parameters,
orderBy: orderBy
});
}
if (!recs) {
recs = record[recsCacheKey] = store.findAll(query);
}
return recs ;
}
}),
unitsDidChange: function() { this.propertyDidChange('units'); }.observes('.units.storeKeys'),
activeUnits: function(){
var us = this.get('units');
if (us == this.previousUnits) return this.previousActiveUnits;
this.previousUnits = us;
var l = us.get('length');
var list = [];
var deleted = [];
var i, u, j, d;
for (i=0; i < l; i++) {
u = us.objectAt(i);
if ( list.indexOf(u) >= 0 ) {
// don't add this again!
}
else {
d = u.get('deletes');
if ( d ) deleted.push(d);
else list.push(u);
}
}
l = deleted.length;
for (i=0; i < l; i++) {
j = list.indexOf(deleted[i])
if (j>=0){
list.splice(j,1);
}
}
this.previousActiveUnits = list;
return list;
}.property('units').cacheable(),
description: function(){
var ret = "";
if (this.get('descriptions') && this.get('descriptions').length()>0)
ret = this.get('descriptions').objectAt(0).get('plural');
return ret;
}.property('descriptions'),
singular: function(){
var ds = this.get('descriptions');
var d = ds ? ds.objectAt(0) : null;
return d ? d.get('singular') : "";
}.property('descriptions'),
plural: function(){
var ds = this.get('descriptions');
var d = ds ? ds.objectAt(0) : null;
return d ? d.get('plural') : "";
}.property('descriptions')
}) ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment