使用来自AJAX调用(XHR请求)的JSON数据填充ember对象的最佳方法是什么?

elc*_*ndo 2 ember.js

我们有一个ember对象,我们想要填充来自AJAX调用的数据:

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

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

});
Run Code Online (Sandbox Code Playgroud)

目前,我们通过以下方式通过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);

            }
        });
    });
Run Code Online (Sandbox Code Playgroud)

在此示例中,JSON数据恢复正常 - "App.WindowDetail.InteriorFinishesDescription"将被填充.

问题是模板没有填充.而且,我不认为这是使用Ember JS时获取JSON数据的正确方法.

以下是车把模板的示例:

<script type="text/x-handlebars">
   <div class="two columns">
      {{App.WindowDetail.InteriorFinishesDescription}}
   </div> 
</script>
Run Code Online (Sandbox Code Playgroud)

Dan*_*rdt 5

要触发Ember的绑定,您需要使用set():

App.WindowDetail.set("InteriorFinishesDescription", r.InteriorFinishesDescription);
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我很高兴你发现我的其他答案很有帮助.肯定有一些不同的持久性选项.在复杂性和能力方面,我会说他们从jQuery.ajax()到Ember RESTEmber Resource再到Ember Data.