2014-10-17 14 views
0

林很新的「JS框架主題」現在我試着如何在emberJS中顯示json?

  1. 獲取從JSON文件數據
  2. 顯示它在一個表

後來我想過濾/對數據進行排序,但我想一步一步來做。

我想出什麼了到目前爲止:

app.js

App = Ember.Application.create(); 

App.Router.map(function() { 
    // put your routes here 
}); 

App.IndexRoute = Ember.Route.extend({ 
    model: function(){ 
     $.ajaxSetup({ 
      async: false 
     }); 
     $.getJSON("json/json_10k.json", function(json) { 
      return json; 
     }); 
    } 
}); 

的index.html(它的一部分):

<script type="text/x-handlebars" id="index"> 
    <ul> 
    {{#each item in model}} 
    <li>:{{item.Id}}</li> 
    {{/each}} 
    </ul> 
</script> 

現在,我沒有看到任何數據/錯誤。也許有人有一個想法,這裏可能是錯的。

+0

你可能想嘗試呼應的JSON的「的getJSON」函數返回之前。然後,您可以將問題本地化。如果數據*加載*不是問題,那麼你應該嘗試製作一個JSFiddle。 – Dodekeract 2014-10-17 12:34:53

+0

感謝@FlorianW的回覆。噸加載是好的(我用Firebug檢查)。我想問題是,getJSON()是異步的,數據不會被返回? – Tream 2014-10-17 12:37:30

+0

我認爲用這種方式編寫的'model'需要一個承諾作爲回報,因爲你在技術上不能以這種方式從回調中返回一個值。我的Ember生鏽了,試試'return $ .getJSON(...' – Joseph 2014-10-17 12:37:35

回答

0

如果你真的想用async:false你可以試着在

$.ajaxSetup({ 
    async: false 
}); 

$.getJSON("json/json_10k.json", function(json) { 
    App.IndexRoute = Ember.Route.extend({ 
    model: json 
    }); 
}); 

其他方式還是回到這樣

App.IndexRoute = Ember.Route.extend({ 
    model: function(){ 
     $.ajaxSetup({ 
      async: false 
     }); 
     return $.getJSON("json/json_10k.json").responseText; 
       // you maybe need to JSON.parse() here 
     // return $.parseJSON($.getJSON("json/json_10k.json").responseText); 
    } 
});