2012-02-02 40 views
2

我們有灰燼的對象,這是我們想從一個AJAX調用數據來填充:使用來自AJAX調用(XHR請求)的JSON數據填充ember對象的最佳方法是什麼?

window.App = Ember.Application.create(
    { rootElement: '#container' } 
); 

App.WindowDetail = Ember.Object.create({ 
     ID: "", 
     Title: "", 
InteriorFinishesDescription: "" 

}); 

目前,我們正在通過Ajax調用以這種方式獲得的JSON數據:

$(window).load(function() { 
     //Call the page method 
     $.ajax({ 
      type: "POST", 
      url: "TestController.aspx", 
      //contentType: "application/json; charset=utf-8", 
      data: "asaw", 
      dataType: "json", 
      success: function (r) { 

       App.WindowDetail.InteriorFinishesDescription = r.InteriorFinishesDescription; 

       alert(App.WindowDetail.InteriorFinishesDescription); 

      } 
     }); 
    }); 

在這個示例中,JSON數據可以很好地再現 - 「App.WindowDetail.InteriorFinishesDescription」被填充。

問題是模板沒有被填充。而且,我不認爲這是使用Ember JS時返回JSON數據的正確方法。

這裏是什麼樣的車把模板看起來像一個示例:

<script type="text/x-handlebars"> 
    <div class="two columns"> 
     {{App.WindowDetail.InteriorFinishesDescription}} 
    </div> 
</script> 
+0

我認爲我已經找到了一個方向和可能的答案來解決我自己的問題:Ember Resource,https://github.com/staugaard/ember-resource。感謝Dan Gebhardt(http://stackoverflow.com/users/664735/dan-gebhardt)對他的回覆(http://stackoverflow.com/questions/9020614/how-to-bind-content-with- json-in-ember-js/9022905#9022905) – elcorando 2012-02-02 19:52:04

回答

5

爲了觸發Ember的綁定,則需要使用set():

App.WindowDetail.set("InteriorFinishesDescription", r.InteriorFinishesDescription); 

BTW,我很高興你發現我的其他答案有幫助。那裏肯定有一些不同的持久性選項。就複雜性和功能而言,我會說他們從jQuery.ajax()到Ember RESTEmber ResourceEmber Data

+0

啊,set()。謝謝!正如你所看到的,我剛開始學習ember。 – elcorando 2012-02-03 22:32:46

+0

很高興幫助!請將問題標記爲已回答(如果是的話),以便讓其他人清楚 - 謝謝。 – 2012-02-04 13:18:04

+0

很好的答案丹。也許將鏈接設置爲「其他答案」。我只是試圖將持久層置於客戶端,而我發現仍然很難找到信息,示例和教程。但我認爲我到了那裏,你的工作是非常有用的:) – Aras 2012-09-13 18:42:16

相關問題