2016-09-13 102 views
-1

我搜索關於這一主題,並嘗試多事情,但到目前爲止一直未能解決我的問題在模板外部JSON數據,所以我創建了一個帳戶,這樣我就可以學習:)無法獲取EmberJS

我正在學習EmberJS,我無法從EmberJS中的路由獲取JSON數據以輸出到實際模板。但是,路由中的Javascript警報會顯示數據。

我打電話給第三方API,通過http從外部獲取域的WHOIS信息。

例JSON輸出

{ 
    "status": [ 
    "clientTransferProhibited https:\/\/icann.org\/epp#clientTransferProhibited" 
    ], 
    "updated_date": [ 
    "2012-12-04T00:00:00" 
    ], 
    "contacts": { 
    "admin": null, 
    "tech": null, 
    "registrant": null, 
    "billing": null 
    }, 
    "nameservers": [ 
    "adns1.apple.com", 
    "adns2.apple.com", 
    "nserver.apple.com", 
    "nserver2.apple.com", 
    "nserver3.apple.com", 
    "nserver4.apple.com", 
    "nserver5.apple.com", 
    "nserver6.apple.com" 
    ], 
    "expiration_date": [ 
    "2021-02-20T00:00:00" 
    ], 
    "creation_date": [ 
    "1987-02-19T00:00:00" 
    ], 
    "raw": [ 
    "\n Domain Name: APPLE.COM\n Registrar: CSC CORPORATE DOMAINS, INC.\n Sponsoring Registrar IANA ID: 299\n Whois Server: whois.corporatedomains.com\n Referral URL: http:\/\/www.cscglobal.com\/global\/web\/csc\/digital-brand-services.html\n Name Server: ADNS1.APPLE.COM\n Name Server: ADNS2.APPLE.COM\n Name Server: NSERVER.APPLE.COM\n Name Server: NSERVER2.APPLE.COM\n Name Server: NSERVER3.APPLE.COM\n Name Server: NSERVER4.APPLE.COM\n Name Server: NSERVER5.APPLE.COM\n Name Server: NSERVER6.APPLE.COM\n Status: clientTransferProhibited https:\/\/icann.org\/epp#clientTransferProhibited\n Updated Date: 04-dec-2012\n Creation Date: 19-feb-1987\n Expiration Date: 20-feb-2021" 
    ], 
    "whois_server": [ 
    "whois.corporatedomains.com" 
    ], 
    "registrar": [ 
    "CSC Corporate Domains, INC." 
    ] 
} 

這裏是我的代碼。

routes/file.js

import Ember from 'ember'; 

export default Ember.Route.extend({ 
    titleToken: "MyTitle", 
    model() { 
     $.ajax({ 
     type: "GET", 
     url: "https://api.who.pm/apple.com", 
     dataType: "json", 
     success: function(jsonData) { 
      alert(JSON.stringify(jsonData)); 
      return jsonData; 
     }, 
     error: function(request, status, error) { 
      console.log("Error! " + request.responseText); 
     } 
     }); 
     } 
}); 

在我templates/file.hbs我已經嘗試了各種東西拿到jsonData輸出,但都沒有成功。我試過{{model}}{{model.data}}和其他一些東西。現在我只是試圖在模板中獲取數據輸出。

當頁面運行時,警報成功啓動。

任何幫助將不勝感激。 謝謝

+0

有你看這裏: http://stackoverflow.com/questions/19982257/ember-js-handlebars-render-vs-outlet-vs-partial-vs-view -vs-control –

+0

@ScottFanetti我剛剛看過那裏,我爲我的經驗不足而道歉,但我不明白這些情況如何適用。我在我的主'application.hbs'中使用'{{outlet}}'來顯示其他路由,而不是在這個特定的頁面中顯示。 – Taha

回答

1

模型鉤子應該返回一個承諾或實際數據,你什麼都不返回。

return jsonData返回成功函數,而不是模型鉤子。當成功掛鉤被調用時,模型掛鉤已經返回。

我可以強烈建議你告訴你自己關於javascript函數,回調函數和閉包函數。還要確保你瞭解this和Promises。

爲了與jQuery一起使用,您需要知道jQuery 2.x .ajax()將返回一個可接受的值,但這不是一個承諾。這在jQuery 3.x中得到解決。所以現在確實使用RSVP.resolve

model() { 
    return Ember.RSVP.resolve($.ajax({ 
    type: "GET", 
    url: "https://api.who.pm/apple.com", 
    dataType: "json", 
    })); 
} 
+0

非常感謝你,我的確需要做更多的研究,瞭解這一切如何運作...... – Taha