如何在Ember.js中将内容与JSON绑定 [英] How to bind content with JSON in Ember.js

查看:96
本文介绍了如何在Ember.js中将内容与JSON绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有的例子都使用了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现在+ 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.appYearController.loadYears (JSON)在一个AJAX的帖子中,它将使用 this.pushObject 创建内容,但这不是 ET .AppYear 对象模式,而我调用 ET.selectedAppYearController.selectedAppYear ,它返回一个未定义的对象,并且真正返回一个 {text:xx,value,xx} 模式。这就是为什么在这种情况下, selectionBinding 也不行。



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

解决方案

查看Ember数据库,它是针对您的用例和相关用例: / p>

https://github.com/emberjs/data


All the examples are using fixed data source in the arraycontroller.content, while I am using dynamic data source which is generated from anther web service and returns a JSON, it won't create an object that I declared in Ember, here is the code sample:

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

As you can see, I will call ET.appYearController.loadYears(JSON) in an AJAX post back, which will create the content by using this.pushObject, but this is not the ET.AppYear object schema, while I call ET.selectedAppYearController.selectedAppYear, it returns an undefined object, and which really returns an object with {text:xx,value,xx} schema. That's why the selectionBinding also won't work in this case.

So what's the typical solution for this to import JSON elements as defined object and put into content?!

解决方案

Take a look at the Ember-data library, it was made for your use-case and related use-cases:

https://github.com/emberjs/data

这篇关于如何在Ember.js中将内容与JSON绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆