Skip to content

Instantly share code, notes, and snippets.

@skizzybiz
Created May 24, 2011 23:56
Show Gist options
  • Save skizzybiz/990025 to your computer and use it in GitHub Desktop.
Save skizzybiz/990025 to your computer and use it in GitHub Desktop.
Record relations
Authoring.ReportTemplate = SC.Record.extend({
// Attributes defined ...
nominations: SC.Record.toMany('Authoring.Nomination', {
isMaster: YES,
inverse: 'reportTemplate'
}),
clone: function() {
var attributes, clone;
attributes = {};
Authoring.ReportTemplate.CLONEABLE_ATTRIBUTES.map(__bind(function(attr) {
return attributes[attr] = this.get(attr);
}, this));
attributes['parent_id'] = this.get('id');
clone = Authoring.store.createRecord(Authoring.ReportTemplate, attributes);
clone.commitRecord();
clone.addObserver('status', this, function() {
var entries, nominations;
if (clone.get('status') !== SC.Record.READY_CLEAN) {
return;
}
if (nominations = this.get('nominations')) {
nominations.map(__bind(function(nom) {
return nom.cloneForTemplate(clone);
}, this));
}
console.log("cloned nominations");
return clone.get('nominations').forEach(function(nom) {
return console.log("" + nom);
});
});
return clone;
}
});
Authoring.Nomination = SC.Record.extend({
// Attributes defined ...
reportTemplate: SC.Record.toOne('Authoring.ReportTemplate', {
isMaster: NO,
key: "report_template_id",
inverse: "nominations"
}),
cloneForTemplate: function(template) {
var attributes, record, _i, _len, _ref;
attributes = {};
_ref = Authoring.Nomination.CLONEABLE_ATTRIBUTES;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
attributes[attr] = this.get(_ref[_i]);
}
attributes.report_template_id = template.get('id');
record = Authoring.store.createRecord(Authoring.Nomination, attributes);
record.commitRecord();
console.log("Created nomination: " + record);
template.get('nominations').pushObject(record);
console.log("Nominations:");
template.get('nominations').forEach(function(n) {
console.log('' + n);
});
return record;
}
});
Authoring.Nomination.CLONEABLE_ATTRIBUTES = [
// ...
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment