簡體   English   中英

EmberJs-將json數據獲取到視圖

[英]EmberJs - Getting json data to the view

這將是非常簡單的事情,但是此刻我在解決如何將json數據放入我的視圖模板中遇到麻煩。

對於我的索引頁面,我有以下代碼:

App.IndexRoute = Ember.Route.extend({
  model: function() {
    var pageContent = App.Content.find(1);

  }
});

這將返回我需要ID為1的頁面內容的JSON。(這在我在Firebug控制台中看到結果時起作用。

它返回以下json

{"content":[{"id":"1","name":"Home","extended":"This is the homepage.","created":"2013-08-05 23:40:55","modified":"2013-08-05 23:40:55"}]}

我有這個視圖設置,但是我無法工作如何從json到視圖中獲取數據:

<script type="text/x-handlebars" id="index">
<div class='index'>
  {{pageContent.entended}}

    The home page
</div>

附言:我只是剛開始使用EmberJS,所以我認為這將非常簡單。

謝謝

您實際上需要從模型掛鈎中返回結果:

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return App.Content.find(1);
  }
});

然后在您的模板中:

<script type="text/x-handlebars" id="index">
  <div class='index'>
  {{model.extended}}
  The home page
</div>

至於您的JSON,則應采用以下格式:

{
  "content": {
    "id":"1",
    "name":"Home",
    "extended":"This is the homepage.",
    "created":"2013-08-05 23:40:55",
    "modified":"2013-08-05 23:40:55"
  }
}

注意刪除的[]

簡單的演示在這里

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM