Skip to content

Instantly share code, notes, and snippets.

@elchele
Last active October 1, 2015 20:40
Show Gist options
  • Save elchele/6c421d44818f81174ce5 to your computer and use it in GitHub Desktop.
Save elchele/6c421d44818f81174ce5 to your computer and use it in GitHub Desktop.
Custom record controller for dynamically loading view metadata based on current record data.
({
/*
File: ./custom/modules/<Module>/clients/base/views/record/record.js
Requires metadata for other view: ./custom/modules/<Module>/clients/base/views/<view>/<view>.php
*/
extendsFrom: 'RecordView',
/**
* Flag for if Model data has sync'd & updated metadata
*/
hasViewBasedMetadata: false,
/**
* @inheritdoc
*/
initialize: function(options) {
this._super('initialize', [options]);
this.model.on('data:sync:complete', this.handleModelData, this);
},
/**
* Handles when the model data comes back from the server
*
* @param method The api call method -- will be "read" for this
* @param model The module's Model data from the server
* @param options
* @param request
*/
handleModelData: function(method, model, options, request) {
// set flag true as model data is back
this.hasViewBasedMetadata = true;
var accountType = this.model.get('account_type'),
newViewName = 'record';
switch(accountType) {
case 'Analyst':
newViewName = 'analyst';
break;
/*
//if multiple types use the same view meta
case 'Customer':
case 'Great Customer!':
newViewName = 'customer';
break;
*/
}
if(newViewName !== '') {
// try to get new view meta
var newMeta = app.metadata.getView(this.module, newViewName);
//if view meta doesn't exist, use current this.meta
this.meta = newMeta || this.meta;
// Call render() now that we've set proper metadata
this.render();
}
},
/**
* @inheritdoc
*/
render: function() {
if(this.hasViewBasedMetadata) {
this._super('render');
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment