Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Though i've had some success learning Ember and how to use it within the context I require, I'm struggling to find a solution to this requirement.

I am using Ember to process an array of field definitions so that I can dynamically generate a form of fields to present to the user. Some of these fields are calculated based on the other fields in the collection - so when generating the fields the view element needs to be able to get the value of one or more of the other fields. I've tried putting a calculated control in the field object, and also in the array controller using propert(@each) but nothing has worked. I've tried too many different permeatations to show my attempted solution here, so I have cleaned the code and left it ready to add the bound functionality required.

I've probably made it harder for myself by using a FieldController but it works so beautifully in using one view_field template to generate a form of view_fields.

<html>
<head>
    <title>Ember.js: Calculate properties using multiple object in an array controller</title>
    <link rel="stylesheet" href="css/normalize.css">    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="js/libs/ember-0.9.5.min.js"></script>

    <script>

    //the application
    App = Ember.Application.create({

    });
    //the model

     App.Field = Ember.Object.extend({
        "name": null,
        "value": null, 
        "controlsource": null
    });
    //an array controller to interface between the data and the views
    App.FieldController = Ember.ArrayController.extend({
        content: [],

    }); 
    App.instanceFieldController=App.FieldController.create();

    //populate the controller with data
    App.instanceFieldController.pushObjects([   
        App.Field.create({name:"field1",value:"1",controlsource:""}),
        App.Field.create({name:"field2",value:"2",controlsource:""}),
        App.Field.create({name:"field3",value:null,controlsource:"field1"}),//this field's value should be calculated from field1
        App.Field.create({name:"field4",value:null,controlsource:"field2"})//this field's value should be calculated from field2
    ]);

    //A collection-view which has a collection of a view, each of which defines the presentation of a fields
    App.FieldCollectionView = Em.CollectionView.extend({            
        itemViewClass: Em.View.extend({
            templateName: "textFieldTemplate"       
        }),
        contentBinding: "App.instanceFieldController.content"
    });

</script>

<h1>Ember.js: Calculate properties using multiple object in an array controller</h1>

<!-- THE FORM TO BE POPULATED WITH THE FIELDS -->
<script type="text/x-handlebars">
    <form action="">
        {{view App.FieldCollectionView}}
        <input type="submit" value="Go">
    </form>
</script>
<!-- THE TEMPLATE FOR HOW TO DISPLAY EACH FIELD -->
<script type="text/x-handlebars" data-template-name="textFieldTemplate">
    <label {{bindAttr for="textField.elementId"}}>{{content.name}}</label>              
    {{view Em.TextField valueBinding="content.value"}}      
</script>

share|improve this question
Can you give an example of what values you want out of this? I'm not getting a real good sense of the desired outcome, here. Also, it's a bad idea to set objects as values in extend as they get shared by any instances, as objects are passed by reference and the reference resides on the prototype. An ArrayController will get an array for its content automatically. – Christopher Swasey Mar 22 at 21:58
Thanks for taking a look. The aim is to create a form dynamically, without knowing which fields prior to execution. Reason: the fields and the exact configuration of them can be changed by the user (and stored in a db). The db will tell Ember the name(N) of the field, its value(V) or its calculated value(C). E.g. N='Firstname',V='Joe'; N='Lastname', V='Blogs'; N='Fullname', C="Firstname + ' ' + Lastname". So the ArrayController will contain 3 elements, the third requiring binding (custom function with an eval statement) to the other two but all three displayed within a CollectionView. – user2192333 Mar 24 at 11:37

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.