We have custom placeholders that are custom images used for different event topics in Open Event Front-end. The custom placeholder are set in the system-images route under admin. The system-images is a dynamic route which changes according the selected topic which brings a challenge to handle dynamic requests to the Orga-server. Lets see how we did it?
Retrieving data from server:
Each custom-placeholder is related to an event-sub-topic, which is related to an event-topic. We fetch a list of all the event-topics available on the system-images route. We also include event-sub-topics in the request & sort the topics by name.
model() { return this.store.query('event-topic', { include : 'event-sub-topics', sort : 'name' }); }, afterModel(model, transition) { this._super(...arguments); if (transition.targetName === 'admin.content.system-images.index') { this.replaceWith('admin.content.system-images.list', model.toArray()[0].id); } }
We override the afterModel() method of the route and redirect to the system-images/list route. We pass the ID of the topic to the dynamic list route, where we use this ID to get all the subtopics of the selected topic.
Handling dynamic route & model:
On the system-images/list route we set the params passed by the system-images route. In the model function we return all the subtopics of the selected topic. On clicking any topic we pass the ID of the topic to the list route where we retrieve all the sub-topics.
model(params) { this.set('params', params); return this.store.findRecord('event-topic', params.topic_id, { include: 'event-sub-topics' }); }
We dynamically render all the subtopics and the related custom-placeholders by looping through the array of the subtopics. We render an image along with a button to change the placeholder for each of the sub-topic. We use a custom modal for changing the placeholder image and for entering details related to the placeholder.
<h4>{{subTopic.name}}</h4> <img src="{{subTopic.placeholder.originalImageUrl}}" class="ui big image" alt={{subTopic.name}}> <div class="ui hidden divider"></div> <button class="ui button primary" {{action 'openModal' subTopic.placeholder}} id="changebutton">{{t 'Change'}}</button>
Saving a custom-placeholder
We use a modal which gets triggered by clicking the change button of the subtopic. The modal allows us to change the image related to the subtopic and other related details like the copyright information and origin information.
The `updatePlaceholder` method gets triggered when the update button is clicked, which sends a PATCH request to the server and updates all the changes made to the custom-placeholder. If the request is successful we close the model and show a success message to the user using the notify service.
updatePlaceholder() { this.get('placeholder').then(placeholder => { placeholder.save() .then(() => { this.set('isOpen', false); this.notify.success(this.l10n.t('Placeholder has been saved successfully.')); }) .catch(()=> { this.notify.error(this.l10n.t('An unexpected error has occured. Placeholder not saved.')); }); }); }
Thank you for reading the blog, you can check the source code for the example here.
Resources
- Official Ember Data documentation
- Ember custom adapter guide