Last active
August 29, 2015 14:17
-
-
Save pcorey/78577ebfad5014c53eb2 to your computer and use it in GitHub Desktop.
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
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' | |
} | |
] | |
}); |
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
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