我有以下兩種模式:燼:異步屬於似乎確實不是解決
型號/ profile.js
import DS from 'ember-data';
export default DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string')
});
型號/ user.js的
import DS from 'ember-data';
export default DS.Model.extend({
profile: DS.belongsTo('profile', {async:true})
});
的用戶模型由用戶服務加載,基於服務器在用戶認證時設置的cookie:
services/user.js
import Ember from 'ember';
export default Ember.Service.extend({
store: Ember.inject.service('store'),
init: function() {
var _this = this;
_this.set('user', null)
if(Cookies.get('uid')) {
_this.get('store').findRecord('user', Cookies.get('uid')).then(function(user) {
console.log(user)
_this.set('user', user)
}, function(err) {
console.warn(err);
Cookies.remove('uid')
_this.set('user', null)
});
} else {
_this.set('user', null)
}
}
});
用戶服務注入到應用控制器:
控制器/ applications.js
import Ember from 'ember';
export default Ember.Controller.extend({
user: Ember.inject.service('user')
});
我有一些調試輸出在我的主模板
模板/應用。 hbs
{{#if user.user}}
<h2 id="title">Welcome to Ember {{user.user}} # {{user.user.id}} # {{user.user.profile}} # {{user.user.profile.firstName}}</h2>
{{else}}
<h2 id="title">Please Log In <a href="/auth/linkedin">Linkedin</a></h2>
{{/if}}
{{outlet}}
,當我進行身份驗證並加載頁面輸出中
Welcome to Ember <[email protected]:user::ember418:5663bfe19d78ce048784a098> # 5663bfe19d78ce048784a098 # <DS.PromiseObject:ember419> #
我可以在控制檯中看到我的API端點的請求,首先爲用戶則配置文件,按預期:
http://localhost:3000/api/users/5663bfe19d78ce048784a098
{
links: {
self: "http://localhost:3000/api/users/5663bfe19d78ce048784a098"
},
data: {
id: "5663bfe19d78ce048784a098",
type: "users",
attributes: {
providers: [{
provider: "linkedin",
id: "ECixxxxqc9",
displayName: "John Smith",
name: {
familyName: "Smith",
givenName: "John"
}
}]
},
links: {
self: "https://stackoverflow.com/users/5663bfe19d78ce048784a098"
},
relationships: {
profile: {
data: {
type: "profiles",
id: "5663c05f5fd3331387307e89"
}
}
}
}
}
http://localhost:3000/api/profiles/5663c05f5fd3331387307e89
{
links: {
self: "http://localhost:3000/api/profiles/5663c05f5fd3331387307e89"
},
data: {
id: "5663c05f5fd3331387307e89",
type: "profiles",
attributes: {
active: true,
yearsExperience: 9,
lastName: "Smith",
firstName: "John"
},
links: {
self: "/profiles/5663c05f5fd3331387307e89"
}
}
}
看起來好像user.user.profile
承諾沒有被解決,因此<DS.PromiseObject:ember419>
輸出和user.user.profile.firstName
沒有值。在模型定義中是否存在某些內容,或者是否存在需要調用以手動通知控制器/模板belongsTo已解決的事情?
更新: 我試圖從配置文件添加到用戶的引用,但是結果都是一樣的
僅供參考,
新/models/profile.js
import DS from 'ember-data';
export default DS.Model.extend({
user: DS.belongsTo('user'),
firstName: DS.attr('string'),
lastName: DS.attr('string')
});
新http://localhost:3000/api/profiles/5663c05f5fd3331387307e89
{
links: {
self: "http://localhost:3000/api/profiles/5663c05f5fd3331387307e89"
},
data: {
id: "5663c05f5fd3331387307e89",
type: "profiles",
attributes: {
active: true,
yearsExperience: 9,
lastName: "Smith",
firstName: "John"
},
links: {
self: "/profiles/5663c05f5fd3331387307e89"
},
relationships: {
user: {
data: {
type: "users",
id: "5663bfe19d78ce048784a098"
}
}
}
}
}
救命稻草了一下這裏抓住一個適當的問題,但它幫助,如果你定義關係的另一面,即將'user'關係添加到'profile'模型中?無論是剛剛在灰燼代碼或還API響應... – Thernys
@Thernys我今天 –
給它當我下班回家一試@Thernys我花了更長的時間比預期的,但設法避開它。不幸的是沒有解決問題,我會更新這個問題,並提供更多關於我在其他人有什麼建議的情況下嘗試的信息。感謝您嘗試幫忙! –