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>
extend
as they get shared by any instances, as objects are passed by reference and the reference resides on the prototype. AnArrayController
will get an array for itscontent
automatically. – Christopher Swasey Mar 22 at 21:58