Skip to content

Instantly share code, notes, and snippets.

@pcorey
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcorey/78577ebfad5014c53eb2 to your computer and use it in GitHub Desktop.
Save pcorey/78577ebfad5014c53eb2 to your computer and use it in GitHub Desktop.
orion.addEntity('categories', {
category: {
type: String,
label: 'Category',
optional: false
},
attributes: {
type: [String],
label: 'Attributes',
optional: true
}
}, {
icon: 'bookmark',
sidebarName: 'Categories',
pluralName: 'Categories',
singularName: 'Category',
tableColumns: [
{
data: 'category',
title: 'Category'
},
{
data: 'attributes',
title: 'Attributes'
}
]
});
orion.admin.addAdminSubscription(orion.subs.subscribe('entity', 'categories'));
var baseSchema = orion.getNewEntitySchema({
product: {
type: String,
label: 'Product',
optional: false
},
category: {
type: String,
autoform: {
options: function() {
return orion.entities.categories.collection.find().fetch().map(function(value) {
return {
value: value._id,
label: value.category
};
});
}
},
optional: false,
label: 'Category'
}
});
function updateReviewsSchema(attributes) {
var reviewsSchema = attributes.reduce(function(schema, attribute) {
schema['reviews.' + attribute] = {
type: Number,
label: attribute,
min: 1,
max: 10,
optional: true,
defaultValue: 1
}
return schema;
}, {
reviews: {
type: Object,
label: 'Reviews',
optional: true
}
});
var schema = _.extend(_.clone(baseSchema), reviewsSchema);
orion.entities.reviews.schema = schema;
orion.entities.reviews.collection.attachSchema(new SimpleSchema(schema), {
replace: true
});
}
if (Meteor.isServer) {
Meteor.methods({
updateReviewsSchema: function(attributes) {
updateReviewsSchema(attributes);
}
})
}
if (Meteor.isClient) {
Tracker.autorun(function() {
var categoryId = Session.get('categoryId');
if (!categoryId) {
return;
}
var category = orion.entities.categories.collection.findOne(categoryId);
Meteor.call('updateReviewsSchema', category.attributes);
updateReviewsSchema(category.attributes);
AutoForm.invalidateFormContext('createEntityForm');
AutoForm.invalidateFormContext('updateEntityForm');
})
Template.adminEntitiesCreate.events({
'change select[data-schema-key="category"]': function(e) {
Session.set('categoryId',$(e.currentTarget).val());
}
});
Template.adminEntitiesCreate.rendered = function() {
Session.set('categoryId',$('select[data-schema-key="category"]').val());
};
Template.adminEntitiesUpdate.events({
'change select[data-schema-key="category"]': function(e) {
Session.set('categoryId',$(e.currentTarget).val());
}
});
Template.adminEntitiesUpdate.rendered = function() {
Session.set('categoryId',$('select[data-schema-key="category"]').val());
};
}
orion.addEntity('reviews', _.clone(baseSchema), {
icon: 'bookmark',
sidebarName: 'Reviews',
pluralName: 'Reviews',
singularName: 'Review',
tableColumns: [
{
data: 'product',
title: 'Product'
}
]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment