I defined several (10+) different models in Ember and set up the resource routes. Everythings works fine. Although every model has different attributes I want to save me time writing redundant code. Let's start with the models first. Here are some examples...
Model: Client
App.Client = DS.Model.extend({
name: DS.attr('string'),
clientId: DS.attr('string'),
clientSecret: DS.attr('string'),
trustedClient: DS.attr('boolean')
});
Model: Group
App.Group = DS.Model.extend({
name: DS.attr('string'),
groupType: DS.attr('string')
});
Model: User
App.User = DS.Model.extend({
email: DS.attr('string'),
password: DS.attr('string'),
});
and more...
Is it possible to use one ( instead of 10+ ) handlebars template to generate labels and textfields for every attribute and value combination?
I got some ideas from this question here on stackoverflow . It already seems quite useful to define a general template as proposed.
<script type="text/x-handlebars" data-template-name="general-table-output">
{{#each metadata}}
<label>{{name}}</label>
{{input valueBinding='name'}}
{{/each}}
</script>
And then we can use the render helper to pass a model to the controller of general-table-output. Here an example for User .
<script type="text/x-handlebars" data-template-name="user">
{{render 'general-table-output' user}}
</script>
Up until here everything seems fine... But the controller does not seem to find the model's attributes.
App.GeneralTableOutput = Ember.ObjectController.extend({
metadata: function() {
var vals = [];
var attributeMap = this.get('content.constructor.attributes');
attributeMap.forEach(function(name, value) {
vals.push(value);
});
return vals;
}.property('content')
});
attributeMap: undefined
getting the attributes is a little tricky when working with Ember Data. This is due to Ember Data overriding the default implementation of Ember.get
so when you call get
on an Ember Data object it skips its internal properties etc going for the model properties (dirty or committed). You'd need to do Ember.get(this.get('content'), 'constructor.attributes')
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.