事實證明這比我想象的要簡單一些。基本上只需要在我的初始化程序中爲我動態生成的每個模型使用Ember.Application.register
,如下所示:
import Ember from "ember";
import request from "ic-ajax";
import DS from "ember-data";
export default {
name: 'model-config',
initialize: function(container, application) {
application.deferReadiness();
request('/path/to/model/config/data').then(function(response) {
Ember.$.each(response.tables, function(modelName, modelCfg) {
var cfg = {};
Ember.$.each(modelCfg, function(fieldName, fieldCfg) {
if(typeof fieldCfg === 'string') {
cfg[fieldName] = DS.attr(fieldCfg);
} else {
cfg[fieldName] = DS[fieldCfg.type](fieldCfg.model);
}
});
application.register('model:' + modelName, DS.Model.extend(cfg));
});
application.advanceReadiness();
});
}
};