Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Ember.js中使用JSON绑定内容_Ember.js - Fatal编程技术网

如何在Ember.js中使用JSON绑定内容

如何在Ember.js中使用JSON绑定内容,ember.js,Ember.js,所有示例都使用arraycontroller.content中的固定数据源,而我使用的是从另一个web服务生成并返回JSON的动态数据源,它不会创建我在Ember中声明的对象, 以下是代码示例: ET.AppYear = Ember.Object.extend({ text:null, value:null }); ET.EmailTypes = Ember.Object.extend();

所有示例都使用arraycontroller.content中的固定数据源,而我使用的是从另一个web服务生成并返回JSON的动态数据源,它不会创建我在Ember中声明的对象, 以下是代码示例:

        ET.AppYear = Ember.Object.extend({
            text:null,
            value:null
        });
        ET.EmailTypes = Ember.Object.extend();

        ET.appYearController = Ember.ArrayController.create({
            content: [],
            loadYears: function (year) {
                if (year != null) {
                    for (var i = -5; i < 5; i++) {
                        this.pushObject({ text: year + i, value: year + i });
                       //.AppYear.create({ text: year + i, value: year + i });
                    }
                }
            }
        });

        ET.selectedAppYearController = Ember.Object.create({
            selectedAppYear: '2011',
            alertChange: function(){
                alert("selected App Year is now " + this.get('selectedAppYear'));
            }.observes('selectedAppYear'),
            isChanged: function () {
                if (this.appYear != null) {
                    this.set('selection',this.get('content'));
                    //LoadETByAppYearETTypeID(this.selectedAppYear, ET.selectedEmailTypesController.emailType.email_template_type_id);
                }
            } .observes('selectedAppYear'),
            AppYear: function() {
                var ay = ET.appYearController.findProperty('text',this.get('selectedAppYear'));
                return ay;
            }.property('selectedAppYear')                
        });
ET.AppYear=Ember.Object.extend({
文本:空,
值:null
});
ET.EmailTypes=Ember.Object.extend();
ET.appYearController=Ember.ArrayController.create({
内容:[],
装载年份:功能(年份){
如果(年份!=null){
对于(变量i=-5;i<5;i++){
this.pushObject({text:year+i,value:year+i});
//.AppYear.create({text:year+i,value:year+i});
}
}
}
});
ET.selectedAppYearController=Ember.Object.create({
所选应用年度:“2011年”,
alertChange:function(){
警报(“所选应用程序年份为现在”+this.get('selectedAppYear');
}.观察(‘选定的应用年度’),
isChanged:函数(){
如果(this.appYear!=null){
this.set('selection',this.get('content');
//LoadeTByAppYeareTypeId(this.selectedAppYear,ET.selectedEmailTypesController.emailType.email\u模板\u类型\u id);
}
}.观察(‘选定的应用年度’),
AppYear:function(){
var ay=ET.appYearController.findProperty('text',this.get('selectedAppYear');
返回日期;
}.property('selectedAppYear')
});
如您所见,我将在AJAX回发中调用
ET.appYearController.loadYears
(JSON),它将使用
this.pushObject
创建内容,但这不是
ET.AppYear
对象模式,而我调用
ET.selectedAppYearController.selectedAppYear
,它返回一个未定义的对象,它实际返回一个具有
{text:xx,value,xx}
模式的对象。这就是为什么在这种情况下,
selectionBinding
也不起作用的原因


那么,将JSON元素作为定义对象导入到内容中的典型解决方案是什么

[2014-02-19:不推荐使用-我不再支持ember rest,因为它过于简单,建议在大多数ember项目中使用ember数据。查看ember数据概述以及此]

我创建了一个非常简单的库来处理Ember中的REST接口:

我还写了一系列关于从Rails使用这个库的文章。第一点是:

我的下一篇文章(希望在今天晚些时候发表)将通过AJAX将数据加载到Ember对象集合中。现在,您可以在示例应用程序以及ember-rest.js自述文件中看到这种行为


如果您想避免使用这个库,您可能仍然希望签出源代码。
Ember.Resource
上的
deserialize()
方法用于导入JSON,也可以与通用的
Ember.Object一起使用

查看Ember数据库,它是为您的用例和相关用例制作的:


Ember数据仍处于测试阶段,但它正在生产应用程序中使用。