我有以下两种模式:
模型/ 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来加载用户模型:
服务/ 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')
});
我的主模板中有一些调试输出
模板/ application.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 <project@model: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: "/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
没有值。我在模型定义中是否缺少某些东西,或者是否需要调用以手动告诉控制器/模板属性已被解析?
更新: 我尝试将配置文件中的引用添加到用户,但结果是相同的
供参考,
new /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"
}
}
}
}
}
答案 0 :(得分:1)
看来我的问题不是我认为的......
我添加了一个&#34;第一个&#34;属性到配置文件,它工作正常,似乎camelCase中的所有属性都没有填充,这是另一个问题
模型与&#34;第一&#34;,/ model / profile.js
$(ele).find("option[selected]").val([]);
端点&#34;第一个&#34;,http://localhost:3000/api/profiles/5663c05f5fd3331387307e89
import DS from 'ember-data';
export default DS.Model.extend({
user: DS.belongsTo('user'),
firstName: DS.attr('string'),
lastName: DS.attr('string'),
yearsExperience: DS.attr('number'),
active: DS.attr('boolean'),
first: DS.attr('string')
});
更新/templates/application.hbs
{
links: {
self: "http://localhost:3000/api/profiles/5663c05f5fd3331387307e89"
},
data: {
id: "5663c05f5fd3331387307e89",
type: "profiles",
attributes: {
active: true,
yearsExperience: 9,
lastName: "Smith",
firstName: "John",
first: "John!"
},
links: {
self: "/profiles/5663c05f5fd3331387307e89"
},
relationships: {
user: {
data: {
type: "users",
id: "5663bfe19d78ce048784a098"
}
}
}
}
}
呈现为
{{#if user.user}}
<h2 id="title">Welcome to Ember {{user.user}} # {{user.user.id}} # {{user.user.profile}} # {{user.user.profile.firstName}} # {{user.user.profile.first}}</h2>
{{else}}
<h2 id="title">Please Log In <a href="/auth/linkedin">Linkedin</a></h2>
{{/if}}
{{outlet}}
这向我表明问题不在于belongsTo解析,而是某些值(具有camelCase的值)未正确设置。我将对此进行一些研究,并在必要时为其发布适当的问题