【问题标题】:How to update Record with local storage adapter如何使用本地存储适配器更新记录
【发布时间】:2013-09-24 10:52:11
【问题描述】:

对于使用LSA 的本地存储适配器,现在我想更新模型中ID 为“ab12”的模型,但我不知道如何操作。我的模型是:

OlapApp.AxisModel = DS.Model.extend({
  uniqueName: DS.attr('string'),
  name: DS.attr('string'),
  hierarchyUniqueName: DS.attr('string'),
  type: DS.attr('string'),//row,column,filter
  isMeasure: DS.attr('boolean'),
  isActive:DS.attr('boolean'), //is added to one of type 
  orderId: DS.attr('number')
});

我的控制器正在尝试:

this.get('store').updateRecord('axisModel',{
   id:item.get('id'),
   orderId:index
});

但我得到一个错误:

Uncaught TypeError: Object [object Object] has no method 'updateRecord' 

我需要更新模型中的 orderId。如果我使用 commit() 我得到一个错误:

 updateSortOrder: function(indexes) {
        var store = this.get('store');
        this.get('getRowItem').forEach(function(item) {
            var index = indexes[item.get('id')];
            if($.isEmptyObject(indexes)) index=0;
            item.set('orderId', index);
            store.commit();
        }, this);
    },

错误是:

Uncaught TypeError: Object [object Object] has no method 'commit' 

这是getRowItem

getRowItem: function(controller, model) {
 return this.get('model').filterProperty('type', 'row');
}.property('model.@each.type'),

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    您可以使用DS.Model.save()。在您的控制器中:

    var model = this.get('content');
    model.setProperties({
       id:item.get('id'),
       orderId:index
    });
    model.save().then(function() {
      alert('saved');
    }, function() {
      alert('not saved');
    });
    

    这是假设Route的model()方法返回了实例,也可以先使用find()

    this.get('store')
    .find('axisModel', id)
    .then(function(model) {
      model.save().then(...)
    });
    

    【讨论】:

    • 首先感谢您的回答。我尝试使用 model.setProperties() 的第一种方式,但我收到错误 Uncaught TypeError: Object [object Object] has no method save 并且我使用第二种方式使用 store 我收到此错误 Uncaught TypeError: Cannot call method lookup of undefined then 方法中的第二种方式,当我 console.log model.get('uniqueName') 我可以看到找到的记录的正确名称。当我使用 DS.Model.save() 时没有发生任何事情,localStorage 中也没有任何变化。
    • 我认为所有这些错误都是因为本地存储适配器不支持最新的 Ember 数据版本
    猜你喜欢
    • 2013-08-09
    • 2015-04-03
    • 2012-08-12
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多