Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 余烬夹具适配器未设置记录ID_Javascript_Ruby On Rails_Ember.js - Fatal编程技术网

Javascript 余烬夹具适配器未设置记录ID

Javascript 余烬夹具适配器未设置记录ID,javascript,ruby-on-rails,ember.js,Javascript,Ruby On Rails,Ember.js,好的,我试着复制一个现有的模型作为一个新的模型实例(因此,除了ID之外,所有属性都是相同的。在我的模板中,我有一个操作,copy,它将列表中该位置范围内的模型传递给控制器,以便进行复制。我的控制器代码如下。我似乎能够创建新记录,但其ID设置为“fixture-0”、“fixture-1”等,并调用.save()(见下文)会导致错误 Uncaught TypeError: Cannot call method 'serialize' of undefined 由于我目前正在开发模型,我正在使用

好的,我试着复制一个现有的模型作为一个新的模型实例(因此,除了ID之外,所有属性都是相同的。在我的模板中,我有一个操作,
copy
,它将列表中该位置范围内的模型传递给控制器,以便进行复制。我的控制器代码如下。我似乎能够创建新记录,但其ID设置为“fixture-0”、“fixture-1”等,并调用
.save()
(见下文)会导致错误

Uncaught TypeError: Cannot call method 'serialize' of undefined 
由于我目前正在开发模型,我正在使用fixture适配器,因为这似乎与问题有关

控制器代码:

REApp.ApplicationController = Ember.ArrayController.extend({
  actions: {
    copy: function(current_model){
      var self = this;
      console.log(current_model);

      var new_record = this.store.createRecord('cycle', {
        railsId: null,
        accountId: current_model._data.accountId,
        //lots more attributes here
      });

      new_record.save();
      console.log(new_record);
    }
  }
});
console.log
来自控制器的呼叫,显示
新记录

a {id: "fixture-0", store: a, container: t, currentState: Object, _changesToSync: Object…}
注意:即使您从控制器中执行
.save()
方法调用,此新记录也是在
id
设置为字符串的情况下创建的。如果未调用
.save()
(因此在控制器中未引发错误),则新记录将显示在余烬检查器的数据面板中,其id设置为如上所述的字符串

这只是我的第三个余烬应用程序(也是我第一个使用1.3.0的应用程序),所以我认为我只是做错了

编辑: 这是模型定义。如果相关的话,我在从控制器返回
varOfModelObject.save();
this.content.save();
时遇到了其他错误,因此def认为它与FixtureAdapter相关

REApp.Cycle = DS.Model.extend({
  //id: DS.attr('number'), not needed with fixtures
  railsId: DS.attr('number'),
  siteId: DS.attr('number'), //all numbers here are ints unless noted
  clientId: DS.attr('number'),
  accountId: DS.attr('number'),
  startAt: DS.attr('datetime'),
  endAt: DS.attr('datetime'),
  chargePerEvent: DS.attr('decimal', {defaultValue: 0.0}),
  createdAt: DS.attr('datetime'),
  updatedAt: DS.attr('datetime'),
  scheduleYaml: DS.attr('string'),
  allDay: DS.attr('boolean'),
  repeat: DS.attr('boolean'),
  exceptionDates: DS.attr('string'),
  additionalDates: DS.attr('string'),
  charge: DS.attr('number'), //decimal in rails
  chargePeriod: DS.attr('string'),
  oldRegularEventId: DS.attr('number'),
  scheduleHuman: DS.attr('string'),
  bagLimit: DS.attr('number'),
  untilDate: DS.attr('number'),
  sendToWebfleet: DS.attr('boolean', {defaultValue: false}),
  contractId: DS.attr('number'),
  hourlyRate: DS.attr('number'), //decimal in rails
  hourlyCharge: DS.attr('number', {defaultValue: 0.0}), //decimal in rails
  doubleEvent: DS.attr('number'),
  //these are used for the simple view 
  weekdays: DS.attr('boolean', {defaultValue: null}),
  weekends: DS.attr('boolean', {defaultValue: null}),
  //data below this needed for associations
  clientName: DS.attr('string'),
  commentBody: DS.attr('string'),
  siteAddress: DS.attr('string'),
  carer1Id: DS.attr('number', {defaultValue: null}),
  carer1Name: DS.attr('string'),
  carer2Id: DS.attr('number', {defaultValue: null}),
  carer2Name: DS.attr('string'),
  users: DS.hasMany('user', {async: true}),
  items: DS.hasMany('item', {async: true})
});

如上所述,在使用装置时,您可以在内存中创建新对象,但它们与硬编码装置对象的区别在于它们的对象ID中有一个前缀
fixture-
。因此,您可能会发现行为有点扭曲-这可能会在更新版本的Ember数据中得到修复,但与此同时,答案似乎是停止使用fixture数据,并在开发过程中尽快转移到实时数据状态。

试试这个,告诉我你得到了什么,是的,在控制台中你会得到一个错误,但它仍然有效,只需按如下方式覆盖方法createrecord:

REApp.ApplicationAdapter = DS.FixtureAdapter.extend({
    mockJSON: function(store, type, record) {
      try {
        record.id = Math.floor((Math.random() * 100) + 1);
        return this._super(store, type, record);
      }
      catch(err) {

      }
    },
  }
);

Alex我不确定我是否理解你的意思。问题是。id可以是字符串,这是一种完全可以接受的格式。它正在分配一个
id
,因为它是夹具数据,并且它知道它没有真正的数据源支持,所以它给了它一个虚拟的唯一id。所以,真正的问题是,你得到的是不能调用序列化的保存时出现未定义错误?您好,是的,我想知道“fixture-$number”是否是默认值,这很好。因此,是的,错误是保存调用返回未定义的记录。这可能是因为新记录只是fixture数据吗?您介意在上面包含您的模型定义吗?这有点大,但我添加了我t以上。@AlexLynham你有没有把它修好?我在对模型进行单元测试时遇到了类似的问题。