Skip to content

Instantly share code, notes, and snippets.

@msmithstubbs
Created December 3, 2012 15:28
Show Gist options
  • Save msmithstubbs/4195718 to your computer and use it in GitHub Desktop.
Save msmithstubbs/4195718 to your computer and use it in GitHub Desktop.
Assets belong to a model
// asset.js updated to have parentId and assetClass
var AssetSchema = new Schema({
urls: [AssetURLSchema],
md5: { type: String },
name: { type: String },
mimetype: { type: String },
family: { type: String },
format: { type: String },
uploaded: { type: Date, 'default': Date.now },
assetClass: { type: String, enum: ['image', 'font'] },
parentId: { type: Schema.Types.ObjectId }
});
// imageAsset.js has a convenience method for finding the parent publication
ImageAsset.prototype.getPublication = function(done) {
return Publication.findById(this.asset.parentId, done);
};
// And a simple example of working with it
// create an asset
var asset = new Asset({
assetClass: 'image',
parentId: publication.id
});
asset.save()
// Later, retrieve it and create an ImageAsset instance
Asset.findById(myId, function(err, asset) {
var image = new ImageAsset(asset);
image.getPublication(function(err, publication) {
// do something with a publication here
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment