Created
December 3, 2012 15:28
-
-
Save msmithstubbs/4195718 to your computer and use it in GitHub Desktop.
Assets belong to a model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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