Ember.js:计算一个对象';在阵列控制器中使用多个对象的阵列中的属性
虽然我已经在学习Ember以及如何在我需要的环境中使用它方面取得了一些成功,但我正在努力找到解决这个需求的方法 我正在使用Ember处理字段定义数组,以便动态生成一种字段形式以呈现给用户。其中一些字段是根据集合中的其他字段计算的,因此在生成字段时,view元素需要能够获取一个或多个其他字段的值。我曾尝试使用propert(@each)在field对象和数组控制器中放置一个计算控件,但没有任何效果。我尝试了太多不同的渗透来显示我尝试的解决方案,所以我清理了代码,并让它准备添加所需的绑定功能 通过使用FieldController,我可能会让自己变得更难,但是使用一个view_字段模板来生成一种view_字段的形式,它的效果非常好Ember.js:计算一个对象';在阵列控制器中使用多个对象的阵列中的属性,ember.js,Ember.js,虽然我已经在学习Ember以及如何在我需要的环境中使用它方面取得了一些成功,但我正在努力找到解决这个需求的方法 我正在使用Ember处理字段定义数组,以便动态生成一种字段形式以呈现给用户。其中一些字段是根据集合中的其他字段计算的,因此在生成字段时,view元素需要能够获取一个或多个其他字段的值。我曾尝试使用propert(@each)在field对象和数组控制器中放置一个计算控件,但没有任何效果。我尝试了太多不同的渗透来显示我尝试的解决方案,所以我清理了代码,并让它准备添加所需的绑定功能 通过使
<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>
Ember.js:在数组控制器中使用多个对象计算属性
//应用程序
App=Ember.Application.create({
});
//模型
App.Field=Ember.Object.extend({
“名称”:空,
“值”:空,
“控制源”:空
});
//用于在数据和视图之间进行接口的阵列控制器
App.FieldController=Ember.ArrayController.extend({
内容:[],
});
App.instanceFieldController=App.FieldController.create();
//用数据填充控制器
App.instanceFieldController.pushObjects([
App.Field.create({name:“field1”,value:“1”,controlsource:“}),
App.Field.create({name:“field2”,value:“2”,controlsource:“}),
create({name:“field3”,value:null,controlsource:“field1”}),//此字段的值应根据field1计算
create({name:“field4”,value:null,controlsource:“field2”})//此字段的值应根据field2计算
]);
//具有视图集合的集合视图,每个视图定义字段的表示形式
App.FieldCollectionView=Em.CollectionView.extend({
itemViewClass:Em.View.extend({
templateName:“textFieldTemplate”
}),
contentBinding:“App.instanceFieldController.content”
});
Ember.js:在数组控制器中使用多个对象计算属性
{{view App.FieldCollectionView}
{{content.name}
{{view Em.TextField valueBinding=“content.value”}
你能举例说明你希望从中得到什么价值吗?在这里,我对期望的结果没有一个真正好的感觉。另外,当对象被任何实例共享时,在
extend
中将对象设置为值是一个坏主意,因为对象是通过引用传递的,引用驻留在原型上。ArrayController
将自动为其内容获取数组。感谢您的关注。其目的是动态创建表单,而不知道执行前的字段。原因:用户可以更改字段及其确切配置(并存储在数据库中)。db将告诉Ember字段的名称(N)、值(V)或计算值(C)。例如:N='Firstname',V='Joe';N='Lastname',V='Blogs';N='Fullname',C=“Firstname+''+Lastname”。因此ArrayController将包含3个元素,第三个元素需要绑定(带有eval语句的自定义函数)到其他两个元素,但这三个元素都显示在CollectionView中。
<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>