首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ember.RSVP总是陷入失败的路由

Ember.RSVP总是陷入失败的路由
EN

Stack Overflow用户
提问于 2013-11-20 10:30:55
回答 1查看 277关注 0票数 0

我使用下面显示的代码在Ember-Model中保存一个新记录。

代码语言:javascript
复制
var saveResult = this.get('model').save();  
saveResult.then (function(value) {$('#statusArea').html('Save succedded.\n'),
                 function(value) {$('#statusArea').html('Save failed.\n')});

在服务器端,一切都按预期运行。我可以看到正确的'post‘消息,并能够将数据保存到数据库中。但是,无论我从服务器返回什么状态(我已经尝试了200、201和204),承诺总是落在失败的例程上。我有两个问题:

1)在上面显示的代码中,我是否正确使用了从Ember-Model返回的Ember.RSVP.promise?

2)如果是,我必须从服务器返回什么才能反映成功?我在服务器端有完全的控制权,基本上可以返回任何需要的东西。

EN

回答 1

Stack Overflow用户

发布于 2013-11-20 10:52:17

是的,你是

创建您自己的rest适配器并覆盖save方法

代码语言:javascript
复制
App.MyRestAdapter = Ember.RESTAdapter.extend({

  saveRecord: function(record) {
    var primaryKey = get(record.constructor, 'primaryKey'),
      url = this.buildURL(record.constructor, get(record, primaryKey)),
      self = this;

  // this is the default implementation, you're blowing up on self.didSaveRecord,
  // you should laugh a little bit noting the todo to implement an api that doesn't
  // return data :)

  //  return this.ajax(url, record.toJSON(), "PUT").then(function(data) {  // TODO: Some APIs may or may not return data
  //    self.didSaveRecord(record, data);
  //    return record;
  //  });

      // technically you could just do
      // return this.ajax(url, record.toJSON(), "PUT");
      // but if you ever want to massage it based on the response in the future, here's the spot
      return this.ajax(url, record.toJSON(), "PUT").then(function(data) {
        return record;
      });

  },
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20086208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档