2013-08-14 2 views
2

이 문제는 컨트롤러, 유닛 테스트, 테스트 및 테스트 중에 관련 객체의 속성에 액세스하려고 할 때와 같이 템플릿 외부에서만 발생한다고 강조하고 싶습니다. 템플릿을 렌더링하면 속성을 잘 얻고 예상대로 작동하는 것처럼 보입니다.관계를 통해 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(); 
    }); 
    }); 
}); 

를로드 할 때

답변

2

당신은 ORM의 주요처럼 데이터 관계에 대한,로드 게으른, 알고하는 then를 사용해야합니다. 이것은로드 된 객체 그래프를 모두 반환 할 필요가 없기 때문에 사용자가 원하는 것을 묻고로드를 남겨 둡니다.

일부 구현은 websql, indexeddb, ajax, websockets 등과 같이 비동기식이기 때문에 데이터가로드되거나 실패한시기를 알려면 then 메서드를 사용해야합니다.

템플릿은 바인딩 인식 기능을 사용하므로 템플릿에서 작동합니다. 변경 사항이 비동기 인 경우에도 나중에 완료되며 바인딩은 알림을 받고 업데이트됩니다.

데모를 업데이트했으며 테스트 결과는 http://jsbin.com/eqojaj/1/edit

입니다.