1

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...

Question:

Is it possible to use one (instead of 10+) handlebars template to generate labels and textfields for every attribute and value combination?

Idea

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.

Controller

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')
});

Problem

attributeMap: undefined

Do you know a way to access an assigned model in the controller and map out all the attributes / values, so that it can be viewed in a generic template?

Community
  • 1
  • 1
Chris
  • 13
  • 2

1 Answers1

0

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').

http://emberjs.jsbin.com/OxIDiVU/394/edit

Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • `var constructor = model.get('firstObject.constructor');` `var a = Em.get(constructor, 'attributes');` this works fine but it still feels like working against ember. – Chris Apr 23 '14 at 15:14
  • Making a generic item like this leads to having to use the innards of Ember, generally I avoid it because then you tend to lose some of the built in functionality, but it all depends on what you're trying to build. – Kingpin2k Apr 23 '14 at 16:44
  • yes... had the same impression. I will try to break things into components and partials. Thanks for your advice. – Chris Apr 23 '14 at 19:35