删除时Ember inFlight错误

时间:2013-12-14 18:05:28

标签: javascript ember.js

我收到此错误并且无法调试它:

Uncaught Error: Attempted to handle event `deleteRecord` on <Todos.Todo:ember309:29> while in state root.loaded.updated.inFlight.

ember和我的JSON API之间的所有事务似乎都运行正常,但如果改变todo的状态,通过重命名或检查完成,然后执行删除,我得到上面的错误。

它们持续存在的变化,如果我删除todo而不进行任何更改,那也是有效的。

以下是一些代码:

路线

Todos.TodosRoute = Ember.Route.extend({
  model: function () {
    return this.store.find('todo');
  }
});

控制器

Todos.TodoController = Ember.ObjectController.extend({
  actions: {
    editTodo: function () {
      this.set('isEditing', true);
    },
    acceptChanges: function () {
      this.set('isEditing', false);

      if (Ember.isEmpty(this.get('model.title'))) {
        this.send('removeTodo');
      } else {
        this.get('model').save();
      }
    },
    removeTodo: function () {
      var todo = this.get('model');
      todo.deleteRecord();
      todo.save();
    },
  },

  isEditing: false,
  isCompleted: function(key, value){
    var model = this.get('model');

    if (value === undefined) {
      // property being used as a getter
      return model.get('isCompleted');
    } else {
      // property being used as a setter
      model.set('isCompleted', value);
      model.save();
      return value;
    }
  }.property('model.isCompleted')
});

适配器

Todos.ApplicationAdapter = DS.RESTAdapter.extend({
    defaultSerializer: "Todos/todosREST",

    serialize: function(record, options) {
        var tmp = get(record, 'store').serializerFor(record.constructor.typeKey).serialize(record, options);

        console.log('ser2:', tmp)
    return tmp
  },
    createRecord: function(store, type, record) {
    var data = {};
    var serializer = store.serializerFor(type.typeKey);

    serializer.serializeIntoHash(data, type, record, { includeId: false });

    var tmp = this.ajax(this.buildURL(type.typeKey), "POST", { data: data });

    // console.log('createRecord:', tmp, data, record);

    return tmp; 
  },
  updateRecord: function(store, type, record) {
    var data = {};
    var serializer = store.serializerFor(type.typeKey);

    serializer.serializeIntoHash(data, type, record);

    var id = get(record, 'id');

    var tmp = this.ajax(this.buildURL(type.typeKey, id), "PUT", { data: data })

    console.log(this.buildURL(type.typeKey, id), "PUT", { data: data })

    return tmp;
  },
  namespace: 'api'
});

串行

Todos.TodosRESTSerializer = DS.RESTSerializer.extend({
    typeForRoot: function(root) {
    var camelized = Ember.String.camelize(root);

    var tmp = Ember.String.singularize(camelized);

    console.log('Camelized:'. tmp)
    return tmp;
  },
  keyForAttribute: function(attr) {
    return Ember.String.underscore(attr);
  },
  serialize: function(record, options) {
    // client to server
    var json = {};

    record.eachAttribute(function(name) {
      json[serverAttributeName(name)] = record.get(name);
    })

    record.eachRelationship(function(name, relationship) {
      if (relationship.kind === 'hasMany') {
        json[serverHasManyName(name)] = record.get(name).mapBy('id');
      }
    });

    console.log('options: ', options)
    if (options && options.includeId) {
      json.id = record.get('id');
    }

    return json;
  },
  serializeIntoHash: function(hash, type, record, options) {
    // hash should be an empty object, so just extend it    
    Ember.merge(hash, this.serialize(record, options));
  },
  extractSingle: function(store, type, payload, id, requestType) {
    // server to client
    console.log('in serializer')

    return this._super(store, type, post_payload, id, requestType);
  },
  extractArray: function(store, type, payload, id, requestType) {
    // server to client
    console.log(payload, type)
    var todos = payload.objects;
    payload = {todos: todos}    

    return this._super(store, type, payload, id, requestType);
  },
});

有人有什么想法吗?

0 个答案:

没有答案