2014-02-14 254 views
0

我需要等待的記錄在我的應用程序加載,但是,這並不工作:等待加載

var sectors = this.get('store').find('sector'); 
    sectors.on('didLoad', function() { 
     console.log("loaded: " + this.get('formLoaded')); 
    }); 

它說,「對象的翻譯:具有‘開’都沒法「, 我做錯了什麼?

更新: 以下菜刀的建議下,我得到一個新的錯誤,因爲我用它來從服務器獲取數據,以填補選擇視圖:

我有一個這樣的控制器:

App.FormController = Em.Controller.extend({ 
sectors: function() { 
    var sectors = this.get('store').find('sector'); 
    return sectors; 
}.property(), 
accesslevels: function() { 
    var accesslevels = this.get('store').find('accesslevel'); 
    return accesslevels; 
}.property(), 
equipments: function() { 
    var equipments = this.get('store').find('equipment'); 
    return equipments; 
}.property(), 
difficulties: function() { 
    var difficulties = this.get('store').find('difficulty'); 
    return difficulties; 
}.property() 

});

,並在模板中我有:

sectors: {{view Ember.Select id="settore" contentBinding="sectors" optionValuePath="content.id" optionLabelPath="content.descrizione"}} 

access levels: {{view Ember.Select id="liv_acc" contentBinding="accesslevels" optionValuePath="content.id" optionLabelPath="content.descrizione"}} 

等; 這有時有效,有時並不是因爲我必須等待來自所有find()的記錄在渲染視圖之前加載;我試着按照Ember文檔中的「on('didLoad')」寫成,但不起作用;你能幫我找到原因嗎?

+0

我的想法是在所有find()完成後將控制器的屬性(formLoad)設置爲true;然後在模板中使用它:{{ifLayad}} ...渲染視圖 –

+0

下面的答案對你有用嗎? – chopper

+0

我已經添加了一個新的答案;就像在那裏解釋的那樣,我一直在想,我有時會得到錯誤,是由於模型加載前的視圖渲染造成的......但是可能我錯了...... –

回答

1

使用由find/findAll返回promise

var sectors = this.get('store').findAll('sector').then(function(data) { 
    console.log("loaded: " + data); 
}); 
0

按照你的建議,以及其他的答案,我終於決定該解決方案加載多模式(使用模式掛鉤的路由處理):

model: function(params) { 
    return Em.RSVP.hash({ 
     settori: this.get('store').findAll('sector'), 
     attrezzature: this.get('store').findAll('equipment'), 
     difficolta: this.get('store').findAll('difficulty'), 
     livelliaccesso: this.get('store').findAll('accesslevel'), 
     sino: [{id: 0, descrizione: "No"}, {id: 1, descrizione: "Sì"}] 
    }); 
}, 
setupController: function(controller, model) { 
    controller.setProperties(model); 
} 

RSVP應該確保所有承諾都在視圖呈現之前完全加載;

在我現在面臨一個奇怪的問題,任何情況下:有時我得到這個錯誤:

Error while loading route: Error: Assertion Failed: The response from a findAll must be an Array, not undefined 

這似乎是不可預知的對我說:有時會發生,然後多次無;我認爲這是第一次發生我加載應用程序; 在文檔中寫道,這可能與模型中的「hasMany」或「belongsTo」關係有關,但我在模型中沒有任何這些關係;

我開發的應用程序與本地自耕農和寧靜的服務器基於節點JS(使用快遞)(運行MySQL服務器的EasyPHP)

有人知道什麼是錯的提供從MySQL加載的數據?