【发布时间】:2014-02-04 09:29:35
【问题描述】:
好的,所以我正在尝试将现有模型复制为新模型实例(因此所有属性都相同,除了它的 ID。在我的模板中,我有一个操作 copy,它将模型传入范围在控制器列表中的那个位置,以便可以复制它。我的控制器代码在下面。我似乎能够创建一个新记录,但它的 ID 设置为“fixture-0”,“fixture-1”等等,并在上面调用.save()(见下文)会导致错误,
Uncaught TypeError: Cannot call method 'serialize' of undefined
由于我目前正在为开发模型存根,因此我正在使用夹具适配器,因为这似乎与问题有关。
控制器代码:
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从控制器调用,显示new_record:
a {id: "fixture-0", store: a, container: t, currentState: Object, _changesToSync: Object…}
注意:即使您从控制器中取出.save() 方法调用,此新记录也会在id 设置为字符串的情况下创建。如果.save() 未被 调用(因此在控制器中没有引发错误),则新记录将出现在 ember 检查器的数据面板中,其 ID 设置为如上所述的字符串。
这只是我的第三个 Ember 应用程序(也是我第一个使用 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})
});
【问题讨论】:
-
Alex 我不确定我是否理解你所说的问题所在。 id 可以是字符串,这是一种完全可以接受的格式。它分配了一个
id,因为它是夹具数据并且它知道它没有得到真实数据源的支持,所以它给它一个虚拟的唯一ID。那么,您在保存时遇到无法调用未定义错误的序列化的真正问题是什么? -
嗨,是的,我想知道'fixture-$number'是否是默认值,很高兴知道。所以是的,错误是保存调用返回未定义的记录。可能是因为新记录只是夹具数据?
-
您是否介意在上面包含您的模型定义
-
有点大,不过我在上面加了。
-
@AlexLynham 你有没有解决这个问题?我在尝试对模型进行单元测试时遇到了类似的问题。
标签: javascript ruby-on-rails ember.js