我有一个我无法弄清楚的wtf问题。我解释 :
我有一个名为 Product 的模型:
var Product = Backbone.RelationalModel.extend(
{
urlRoot: Backbone.rootApiUrl + '/products',
defaults: {
id: '',
name: '',
description: '',
current_price: '',
categories: '',
duration: '',
shipping_cost: '',
start_date: '',
user_id: null,
is_buy_it_now: ''
},
relation: [{
type: Backbone.HasOne,
key: 'user_id',
relatedModel: User,
autoFetch: true
}]
});
我的字段user_id
是一个用户的外键(也是主干中的模型):
var User = Backbone.RelationalModel.extend(
{
urlRoot: Backbone.rootApiUrl + '/users',
defaults: {
id: '',
name: '',
email: '',
firstname: '',
lastname: '',
password: '',
card_nb: '',
cart_total: ''
}
});
我的 fetch 关系在每个单独的模型中都能完美运行。但是当我在和我的用户模型之间建立关系时Product.user_id
,会发生 wtf 错误。
我已经尝试了所有可能的方法:所有的键(keySource
, keyDestination
...... ,所有可能的值)。我什至在 Relation Lib 中设置了断点,以便能够看到发生了什么……没什么好说的。甚至 StackOverflow 都不知道这个错误:P(或者我搜索得很糟糕)
最后的问题是:
- 我的产品由 API 正确发送并在 Backbone (OK) 中设置。
- 该字段
user_id
首先由 Product API 响应设置为一个数字,然后由 Relational 设置为 NULL 而不是由对象 User 替换(为什么?) user_id
当我查看我的网络调试器时,Relational 使用数字 (OK)调用 User 对象的 API(这里没问题)。服务器响应良好,JSON 中的正确用户(OK)。但是 Relational 不会将此响应对象与 field 绑定,该字段user_id
现在是 .... NULL (NOT OK)。- 所以我丢失了一个 User 对象(但如果我在我的 autoFetch 中放置一个成功函数,我可以 console.log() 它),没有正确链接到他的父 Product 与 field
user_id
。
- 所以我丢失了一个 User 对象(但如果我在我的 autoFetch 中放置一个成功函数,我可以 console.log() 它),没有正确链接到他的父 Product 与 field
编辑:这里有一个 JSFiddle http://jsfiddle.net/gjdass/WNWrm/(注意,API 响应时间可能很长,大约 10 秒,这是正常的)。
在此先感谢:/ 抱歉,您为此花费了这么多时间。