【问题标题】:How to update record in local storage using ember data and localstorage adapter?如何使用 ember 数据和本地存储适配器更新本地存储中的记录?
【发布时间】:2013-08-09 01:07:55
【问题描述】:

我是 emberjs 的新手,正在制作一个简单的 CRUD 应用程序。我正在使用 ember 数据和 localstorage-adapter 将记录保存在浏览器的本地存储中。

我正在尝试使用 localstorage-adapter 更新记录,但它抛出错误。

我在这里列出了我的代码:

updatecontact: function(){//save data in local storage
        var fname = this.obj_form_edit_data.get('cont_data.fname');
        var lname = this.get('cont_data.lname');
        var email = this.get('cont_data.email');
        var contactno = this.get('cont_data.contactno');
        var gendertype = ((this.get('isMale') == true) ? true : false);
        var contactype = $(".selectpicker").val();
        Grid.ModalModel.updateRecords({ 
          fname: fname,
          lname: lname,
          email: email,
          contactno: contactno,
          gendertype: gendertype,
          contactype: contactype
        });
        this.get('store').commit();             
    }

使用上面的代码我收到以下错误:

Uncaught TypeError: Object function () {
if (!wasApplied) {
  Class.proto(); // prepare prototype...
}
o_defineProperty(this, GUID_KEY, undefinedDescriptor);
o_defineProperty(this, '_super', undefinedDescriptor);
var m = meta(this);
m.proto = this;
if (initMixins) {
  // capture locally so we can clear the closed over variable
  var mixins = initMixins;
  initMixins = null;
  this.reopen.apply(this, mixins);
}
if (initProperties) {
  // capture locally so we can clear the closed over variable
  var props = initProperties;
  initProperties = null;

  var concatenatedProperties = this.concatenatedProperties;

  for (var i = 0, l = props.length; i < l; i++) {
    var properties = props[i];

    Ember.assert("Ember.Object.create no longer supports mixing in other definitions, use createWithMixins instead.", !(properties instanceof Ember.Mixin));

    for (var keyName in properties) {
      if (!properties.hasOwnProperty(keyName)) { continue; }

      var value = properties[keyName],
          IS_BINDING = Ember.IS_BINDING;

      if (IS_BINDING.test(keyName)) {
        var bindings = m.bindings;
        if (!bindings) {
          bindings = m.bindings = {};
        } else if (!m.hasOwnProperty('bindings')) {
          bindings = m.bindings = o_create(m.bindings);
        }
        bindings[keyName] = value;
      }

      var desc = m.descs[keyName];

      Ember.assert("Ember.Object.create no longer supports defining computed properties.", !(value instanceof Ember.ComputedProperty));
      Ember.assert("Ember.Object.create no longer supports defining methods that call _super.", !(typeof value === 'function' && value.toString().indexOf('._super') !== -1));

      if (concatenatedProperties && indexOf(concatenatedProperties, keyName) >= 0) {
        var baseValue = this[keyName];

        if (baseValue) {
          if ('function' === typeof baseValue.concat) {
            value = baseValue.concat(value);
          } else {
            value = Ember.makeArray(baseValue).concat(value);
          }
        } else {
          value = Ember.makeArray(value);
        }
      }

      if (desc) {
        desc.set(this, keyName, value);
      } else {
        if (typeof this.setUnknownProperty === 'function' && !(keyName in this)) {
          this.setUnknownProperty(keyName, value);
        } else if (MANDATORY_SETTER) {
          Ember.defineProperty(this, keyName, null, value); // setup mandatory setter
        } else {
          this[keyName] = value;
        }
      }
    }
  }
}
finishPartial(this, m);
delete m.proto;
finishChains(this);
this.init.apply(this, arguments);

} 没有方法“updateRecords”

我正在使用以下代码创建工作正常的新记录:

savecontact: function(){//save data in local storage
        var fname = this.obj_form_edit_data.get('cont_data.fname');
        var lname = this.obj_form_edit_data.get('cont_data.lname');
        var email = this.obj_form_edit_data.get('cont_data.email');
        var contactno = this.obj_form_edit_data.get('cont_data.contactno');
        var gendertype = ((this.get('isMale') == true) ? true : false);
        var contactype = $(".selectpicker").text();
        Grid.ModalModel.createRecord({  
          fname: fname,
          lname: lname,
          email: email,
          contactno: contactno,
          gendertype: gendertype,
          contactype: contactype
        });
        this.get('store').commit(); 
    }

【问题讨论】:

  • 你得到什么错误?
  • @intuitivepixel:我已经用错误更新了我的问题。
  • 您可以先获取要更新的记录,更改其属性并调用save。即使updateRecords 将存在于 LSAdapter 中,它也需要您确定应该编辑的记录,并且可能会采用对象数组而不是您的符号中的单个对象。

标签: ember.js ember-data


【解决方案1】:

你使用updateRecords作为复数形式,应该是updateRecord

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 2015-07-02
    • 1970-01-01
    • 2017-09-01
    相关资源
    最近更新 更多