将Emberjs recordArray模型转换为JSON

时间:2015-02-28 15:11:58

标签: json ember.js ember-data

免责声明:我是Ember的新手。

话虽如此,我正在尝试将Ember recordArray转换为JSON字符串,以便我可以将其发送到我的服务器进行处理。

我有以下Ember模型:

Loads.Load = DS.Model.extend({
    pickupStartTime: DS.attr('string'),
    pickupEndTime: DS.attr('string'),
    pickupLocation: DS.attr('string'),
    pickupGeocode: DS.attr('string'),
    pickupWeight: DS.attr('number'),
    pickupTicket: DS.attr('string'),
    dropoffStartTime: DS.attr('string'),
    dropoffEndTime: DS.attr('string'),
    dropoffLocation: DS.attr('string'),
    dropoffGeocode: DS.attr('string'),
    dropoffWeight: DS.attr('number'),
    dropoffTicket: DS.attr('string'),
    commodity: DS.attr('string'),
    isCompleted: DS.attr('boolean'),
    shortPickupStartTime: function(){
        var time = this.get('pickupStartTime');
        if(time){
            var split = time.split(" ");
            return split[4];    
        }else{
            return " ";
        }
    }.property('pickupStartTime'),
    shortPickupEndTime: function(){
        var time = this.get('pickupEndTime');
        if(time){
            var split = time.split(" ");
            return split[4];    
        }else{
            return " ";
        }
    }.property('pickupEndTime'),
    shortDropoffStartTime:function(){
        var time = this.get('dropoffStartTime');
        if(time){
            var split = time.split(" ");
            return split[4];    
        }else{
            return " ";
        }
    }.property('dropoffStartTime'),
    shortDropoffEndTime: function(){
        var time = this.get('dropoffEndTime');
        if(time){
            var split = time.split(" ");
            return split[4];    
        }else{
            return " ";
        }
    }.property('dropoffEndTime')
});

我的控制器中有以下操作:

var self = this;

Ember.RSVP.hash({
   //loads is an array of records
   loads: this.store.find('load')
}).then(function (data) {      
   $.ajax({
       type: "POST",
       data: { Loads: JSON.stringify(data.loads) },
       url: "api/buildexcelsheet",
       contentType: "application/json",
       success: function (message) {
           self.set('uri', message);
       }
   });
});

当我从控制器运行此操作时,出现以下错误:

Uncaught Error: Assertion Failed: TypeError: Converting circular structure to JSON

因为我是Ember的新手,所以我不确定我做错了什么。

非常感谢任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:3)

我最终自己解决了这个问题:

loads: store.find('load').then(function (units) {
    return units.map(function (x) { return x.toJSON(); })
});

这就是诀窍!