2013-02-28 7 views
8

この質問への回答とこの全面的な質問は廃止です。 EMBER DATAには多くの変更がありました。私はこの小さなエンバーアプリケーションを持っているhttp://guides.emberjs.com/v1.11.0/models/

Ember.jsのfind()結果の長さにアクセスする方法

window.App = Ember.Application.create(); 

App.Store = DS.Store.extend({ 
    revision: 11, 
    adapter: DS.FixtureAdapter({ 
    simulateRemoteResponse: false 
    }) 
}); 

App.Model = DS.Model.extend({ 
    title: DS.attr('string') 
}); 
App.Model.FIXTURES = []; 

App.ready = function() { 
    console.dir(App.Model.find().get('length')); 
    App.Model.createRecord({id: 1, title: "TEST"}); 
    console.dir(App.Model.find().get('length')); 
    console.dir(App.Model.find(1).get('title')); 
}; 

私はしかしget('length')通話の両方が0私は何をしないのですが返さconsole.dir(App.Model.find(1).get('title')で右のタイトルを取得これを読んで?あなたがApp.Model.find()を行う際に基本的に、それはあなたのインスタンスを返し、その理由は、データがロードされていても前にget("length")を起動していることかもしれませんhttp://jsbin.com/uxalap/6/edit

答えて

9

:ここ

は、(非)作業jsbinですRecordArrayのデータはありませんが、データはバックグラウンドでデータベースで照会され、データを取得します。データがロードされると、実際の長さが表示されます。

isLoadedのオブザーバーを追加しようとする可能性がありますあなたはまた、代わりに、間接プロパティ観察 'didLoad' イベントを使用することができ

record = App.store.findQuery(App.Model); 
alertCount = function(){ 
    if(record.isLoaded){ 
    alert(record.get("length")); 
    } 
}; 
Ember.addObserver("isLoaded", record, alertCount); 
+0

を次のようにY:record.one( 'didLoad'、関数(){アラート(record.get( '長さ'))})。 –

+0

@ChristopherSwasey私は次のようになります:エラー:見つかりません at RSVP.onerrorDefault – SuperUberDuper

+2

このQAは2歳で、Ember Dataはその時に大きく変化しました。私は、SOの質問/回答にフラグを立て、時代遅れの方法があることを願っています。 –

関連する問題