이 문제는 컨트롤러, 유닛 테스트, 테스트 및 테스트 중에 관련 객체의 속성에 액세스하려고 할 때와 같이 템플릿 외부에서만 발생한다고 강조하고 싶습니다. 템플릿을 렌더링하면 속성을 잘 얻고 예상대로 작동하는 것처럼 보입니다.관계를 통해 ember-data의 속성에 액세스하기 (템플릿에 없음)
여기 JS Bin의 간단한 예제 http://jsbin.com/ihumuk/4/edit이 내 문제를 재현합니다. 통과 테스트는 속성이 예상대로 템플릿에 액세스하여 렌더링되었음을 나타냅니다. 실패한 테스트 결과 get
으로 속성에 액세스하려고하면 null
이 표시됩니다. 여기에는 전혀 공상적인 것이 없지만 그것이 왜 돌아가는지 이해할 수 없습니다. null
.
App.ApplicationRoute = Em.Route.extend({
model: function() {
return App.Foo.find();
}
});
App.Store = DS.Store.extend({
adapter: DS.FixtureAdapter.create()
});
App.Foo = DS.Model.extend({
name: DS.attr("string"),
/**
* The subject under test
*/
childName: function() {
return this.get("child.name");
}.property("child.name"),
child: DS.belongsTo("App.Bar")
});
App.Bar = DS.Model.extend({
name: DS.attr("string")
});
App.Foo.FIXTURES = [{
id: 1,
name: "Fred",
child: 3
}, {
id: 2,
name: "Barney",
child: 4
}];
App.Bar.FIXTURES = [{
id: 3,
name: "Pebbles"
}, {
id: 4,
name: "Bam Bam"
}];
이 통과 : 여기
는 JS 빈 예의 프로그램 부분이다.test("Child name is rendered", function() {
expect(1);
visit("/").then(function() {
ok(find("div:contains(Pebbles)").length);
});
});
이것은 실패합니다.
test("Child name is accessed", function() {
expect(2);
var foo = App.Foo.find(1);
equal(foo.get("childName"), "Pebbles");
equal(foo.get("child.name"), "Pebbles");
});
이 문자 또는 무언가를 잊고 바보 같은 간단한 일/수있다,하지만 난 잠시 동안 명확하게 생각하는 좌절감에 너무 멀리 자신을 구동 것 같아요. 모든 도움을 미리 감사드립니다. 데이터가 타다 남은 데이터에서
asyncTest("Child name is accessed", function() {
expect(2);
// load the data from server
App.Foo.find(1).then(function(foo) {
// the child id is 3, we need to fetch the remaining data
// and this is async, because of the ajax request
foo.get("child").then(function(child) {
equal(child.get("name"), "Pebbles");
// childName call child.name, but since the
// data is loaded, isn't necessary to use a second then
equal(foo.get("childName"), "Pebbles");
start();
});
});
});
를로드 할 때