gpt4 book ai didi

javascript - Ember 得不到某些属性

转载 作者:行者123 更新时间:2023-11-29 15:40:12 24 4
gpt4 key购买 nike

当从 UserController 运行以下命令时在谷歌浏览器上,ember-couchdb-kit-0.9 , Ember 数据 v1.0.0-beta.3-56-g8367aa5 , Ember v1.0.0 , 和 this couchdb adapter :

customerSignUp: function () {
var model = this.get('model');
var customer = this.get('store').createRecord('customer', {
description: 'Why hello sir',
user: model
});
customer.save().then(function() {
model.set('customer', customer);
model.save();
});
}

使用这些模型:

App.User = App.Person.extend({
name: DS.attr('string'),
customer: DS.belongsTo('customer', {async: true })

App.Customer = DS.Model.extend({
user: DS.belongsTo('user', {async: true}),
description: DS.attr('string')
});

用户和客户都没有正确设置他们的关系(在 Ember 调试器中,用户有 null,客户有 <computed>,而不是某种 <EmberPromiseObject>,这是他们在工作时所拥有的) .只有当所讨论的对象被持久化时才会发生这种情况。如果save()调用被省略,两者都正确设置了关系,但当然数据库还没有用这些信息更新。每当保存发生时,关系都会被空条目覆盖。

我发现问题出在适配器的serializeBelongsTo函数,我现在已将副本更改为以下内容:

serializeBelongsTo: function(record, json, relationship) {
console.log("serializeBelongsTo");
console.log(record.get('user'));
console.log(json);
console.log(relationship);
var attribute, belongsTo, key;
attribute = relationship.options.attribute || "id";
console.log(attribute);
key = relationship.key;
console.log(key);
belongsTo = Ember.get(record, key);
console.log(belongsTo);
if (Ember.isNone(belongsTo)) {
return;
}
json[key] = Ember.get(belongsTo, attribute);
console.log(Ember.get(belongsTo, attribute));
console.log(json);
if (relationship.options.polymorphic) {
return json[key + "_type"] = belongsTo.constructor.typeKey;
}
else {
return json;
}
}

attribute , belongsTo , 和 key所有记录都是正确的,但是 console.log(Ember.get(belongsTo, attribute));返回 undefined ,我试着改成 console.log(Ember.get(Ember.get(belongsTo, 'content'), attribute));console.log(belongsTo);告诉我id属性隐藏在 content 中目的。附件是显示我的意思的屏幕截图。 screenshot from 2013-12-07 18 15 52虽然更改并没有解决问题,但我一直收到 undefined .无论我使用什么方法尝试从 belongsTo 中获取 id对象,我总是得到 nullundefined .以下是我尝试获得的一些示例 content出对象:

var content = belongsTo.content;
var content = Ember.get(belongsTo, 'content');
var content = belongsTo.get('content');

console.log(json);返回 Object {description: "Why hello sir", user: undefined}

这是一个显示相关输出的 pastebin:http://pastebin.com/v4mb3PJ2

更新

一个非常困惑的更新!

当我从不同的函数保存模型时:

saveModel: function() {
this.get('model').save().then(
function( data, textStatus, jqXHR ) {
console.log('Saved successfully.');
},
function( jqXHR, textStatus, errorThrown ) {
console.log(jqXHR);
console.log(errorThrown);
console.log(textStatus);
}
);
}

模型已正确保存。 serializeBelongsto 中的所有内容完全按预期工作。

这是一个不同的 pastebin,显示了这种情况下的输出:http://pastebin.com/Vawur8Q0

最佳答案

我发现了问题。基本上 serializeBelongsTo 中的 belongsTo 对象在被引用时并没有真正解析,这是我通过查询 isFulfilled 发现的。所以我通过这种方式保存边来实现:

function saveOn (target, attribute) {
target.addObserver(attribute, function () {
if (target.get(attribute)) {
console.log("Inside with %@".fmt(attribute));
target.removeObserver(attribute);
Ember.run.once(target, function() {
target.save();
});
}
});
};

customerSignUp: function () {
var model = this.get('model');
var customer = this.get('store').createRecord('customer', {
description: 'Why hello sir'
});
customer.save().then(function () {
model.set('customer', customer);
customer.set('user', model);
saveOn(customer, 'user.isFulfilled');
saveOn(model, 'customer.isFulfilled');
});
}

现在一切都很顺利。不过,serializeBelongsTo 考虑到这一点可能是个好主意。这行:console.log(Ember.get(belongsTo, 'isFulfilled')); 在我的例子中出现了 false。在记录的创建和序列化之间只是存在某种竞争条件!

不过,我想让我的 saveOn 函数返回一个 promise ,然后我可以使用它来将多个 saveOn 链接在一起。这样我就不必执行 customer.save() 来确保填充了 id。

关于javascript - Ember 得不到某些属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20456890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com