Skip to content

Instantly share code, notes, and snippets.

@thomaslang
Created September 2, 2009 15:08
Show Gist options
  • Save thomaslang/179753 to your computer and use it in GitHub Desktop.
Save thomaslang/179753 to your computer and use it in GitHub Desktop.
// ==========================================================================
// Project: BB.listSelectionController
// Copyright: ©2009 My Company, Inc.
// ==========================================================================
/*globals BB */
/** @class
(Document Your Controller Here)
@extends SC.ObjectController
*/
BB.listSelectionController = SC.ObjectController.create(
/** @scope BB.listSelectionController.prototype */ {
contentBinding: 'BB.listController.selection',
name: function(){
var content = this.get('content');
return content ? content.firstObject().getPath('user.name') : "";
}.property().cacheable(),
//minute: function(){
// var date = this.get('date');
// if (date) {
// console.log(date.get('minute'));
// return date.get('minute');
// }
// else {
// console.log('date is null');
// return null;
// }
//}.property(),
//contentDidChange: function(){
// console.log('content did change');
// this.notifyPropertyChange('minute');
//}.observes('content')
}) ;
// the view:
date: SC.LabelView.create({
layout: { top: 50, height: 20, left: 100, right: 10 },
textAlign: SC.ALIGN_RIGHT,
valueBinding: 'BB.listSelectionController.minute'
})
// the model -- look under computed properties
// ==========================================================================
// Project: BB.Unit
// Copyright: ©2009 My Company, Inc.
// ==========================================================================
/*globals BB */
/** @class
(Document your Model here)
@extends SC.Record
@version 0.1
*/
BB.Unit = SC.Record.extend(
/** @scope BB.Unit.prototype */ {
couchView: "units",
primaryKey: "_id",
date: SC.Record.attr('SC.DateTime', { isRequired: YES }),
deletes: SC.Record.toOne('BB.Unit'),
/////////////////////////////////////////////////////
// relations
//
user: SC.Record.toOne('BB.User', { isRequired: YES }),
library: SC.Record.toOne('BB.Library', { isRequired: YES }),
concept: SC.Record.toOne('BB.Concept', { isRequired: YES }),
attributes: SC.Record.toManyByKey('BB.Attribute', 'unit', {orderBy: "date DESC"}),
associations: SC.RecordAttribute.create({
toType: function(record, key, value) {
var conditions = 'unit1 = {record} OR unit2 = {record}';
var parameters = {record: record};
var orderBy = "date";
var recordType = BB.Association;
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 ;
}
}),
/////////////////////////////////////////////////////
// computed properties
//
userName: function(){
//console.log('user name');
var user = this.get('user');
return user ? user.get('name') : null;
}.property(),
minute: function(){
var date = this.get('date');
if (date) {
console.log(date.get('minute'));
return date.get('minute');
}
else {
console.log('date is null');
return null;
}
}.property(),
attributeList: function(){
var ps = this.getPath('concept.activeProperties');
var list = [];
var l = ps ? ps.length : 0;
var i, attr, entry, p;
for (i=0; i < l; i++) {
p = ps.objectAt(i);
attr = this.attributeForProperty(p);
entry = SC.Object.create({
p: p,
property: p.get('description'),
attribute: attr ? attr.get('content') : "",
attributeDidChange: function(){
BB._addAttributeForProperty(this.p, this.attribute);
}.observes('attribute')
});
if ( list.indexOf(entry) < 0 ) {
list.push(entry);
}
}
return list;
}.property().cacheable(),
attributeForProperty: function(property) {
var as = this.get('attributes');
var l = as.get('length');
var i, a;
for (i=0; i < l; i++) {
a = as.objectAt(i);
if ( a.get('property') == property ) return a;
};
return null;
},
associationList: function(){
console.log('associationList computed');
var unit = this;
var concept = this.get('concept');
var rs = concept.get('relations');
var l = rs ? rs.length() : 0;
var treeItemChildren = [];
var i, j, k, t, relation, as, a, group, groupTitle,
children, associatedUnit, child, tree, autorelation, turns, inverse;
// loop through relations
for (i=0; i < l; i++) {
relation = rs.objectAt(i);
as = this.associationsForRelation(relation);
k = as ? as.length : 0;
autorelation = (relation.get('concept1')==relation.get('concept2')) ? YES : NO ;
turns = (autorelation) ? 2 : 1 ;
for (t=0; t < turns; t++) {
children = [];
if (autorelation){
inverse = (t==1) ? NO : YES;
}
else {
inverse = (concept == relation.get('concept2'));
}
// loop through associations for this relation
// collecting children
for (j=0; j < k; j++) {
a = as.objectAt(j);
associatedUnit = (inverse) ? a.get('unit1') : a.get('unit2');
if (associatedUnit && associatedUnit != this) {
child = SC.Object.create({
association: a,
title: associatedUnit.get('display'),
actionClassName: 'minus',
performAction: function(view){
BB._deleteAssociation(this.association);
}
});
children.push(child);
}
}
// determine direction of relation
// get groupTitle
groupTitle = (inverse) ? relation.get('pluralInverse') : relation.get('plural');
// build group
group = SC.Object.create({
unit: unit,
relation: relation,
inverse: inverse,
treeItemIsExpanded: YES,
title: groupTitle,
treeItemChildren: children,
actionClassName: 'plus',
performAction: function(view){
BB.popupPicker(this.unit, this.relation, this.inverse, view);
}
});
// push group to list
treeItemChildren.push(group);
};
}
tree = SC.Object.create({
treeItemIsExpanded: YES,
title: 'root',
treeItemChildren: treeItemChildren
});
return tree;
}.property().cacheable(),
associationsForRelation: function(relation){
var as = this.get('associations');
var l = as.get('length');
var list = [];
var deleted = [];
var i, a, j, d;
for (i=0; i < l; i++) {
a = as.objectAt(i);
if ( list.indexOf(a) >= 0 ) {
// don't add this again!
}
else {
if ( a.get('relation') == relation ) {
d = a.get('deletes');
if ( d ) deleted.push(d);
else list.push(a);
}
}
}
l = deleted.length;
for (i=0; i < l; i++) {
j = list.indexOf(deleted[i])
if (j>=0){
list.splice(j,1);
}
}
return list;
},
display: function(){
var ps = this.getPath('concept.properties');
var p = ps ? ps.objectAt(0) : null;
var a = p ? this.attributeForProperty(p) : null;
var display = a ? a.get('content') : "";
return display;
}.property().cacheable(),
/////////////////////////////////////////////////////
// change notifiers
//
attributesContentDidChange: function() {
this.notifyPropertyChange('display');
this.notifyPropertyChange('attributeList');
},
associationsContentDidChange: function() {
this.notifyPropertyChange('associationList');
},
propertiesContentDidChange: function() {
this.notifyPropertyChange('attributeList');
},
/////////////////////////////////////////////////////
// setup
//
_setupObservers: function() {
var attributes = this.get('attributes');
attributes.addObserver('[]', this, this.attributesContentDidChange);
var associations = this.get('associations');
associations.addObserver('[]', this, this.associationsContentDidChange);
var properties = this.getPath('concept.properties');
properties.addObserver('[]', this, this.propertiesContentDidChange);
},
init: function() {
sc_super();
this.invokeLater(this._setupObservers, 1);
}
}) ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment