2013-05-16 18 views
0

我是一個笨dev的,我努力學習ember.js。Ember.js AJAX模式

我一直在試圖讓一個模型一起,這將使一個AJAX調用服務器,在一個XML文件中提取,分析數據,並將其返回。

以下是我的模型代碼:

App.Statement = Ember.Object.extend(); 
App.Statement.reopenClass({ 
    all: function() { 
     var transactions = {}; 
     // FROM: http://jquerybyexample.blogspot.com/2012/04/read-and-process-xml-using-jquery-ajax.html 
     $.ajax({ 
      type: "GET", 
      url: "http://www.example.com/transactions.xml", 
      dataType: "xml", 
      success: function(xml){ 
       $(xml).find('transaction').each(function(i,v){ 
        transactions[i].id  = $(this).attr('id'); 
        transactions[i].vendor = $(this).find('vendor').text(); 
        transactions[i].date = $(this).find('date').text(); 
        transactions[i].spent = $(this).find('spent').text(); 
       }); 
      }, 
      error: function() { 
       console.log("An error occurred while processing XML file."); 
      } 
     }); 
     return transactions; 
    } 
}); 

下面是我的XML文件(transactions.xml)的內容:

<?xml version="1.0" encoding="UTF-8" ?> 
<statement> 
    <transaction id="123456"> 
     <vendor>WH Smiths</vendor> 
     <date>2013-05-01</date> 
     <spent>10.00</spent> 
    </transaction> 
    <transaction id="123457"> 
     <vendor>Gap</vendor> 
     <date>2013-05-02</date> 
     <spent>39.99</spent> 
    </transaction> 
    <transaction id="123458"> 
     <vendor>DSG PLC</vendor> 
     <date>2013-05-03</date> 
     <spent>1024.99</spent> 
    </transaction> 
    <transaction id="123459"> 
     <vendor>Tesco</vendor> 
     <date>2013-05-06</date> 
     <spent>23.35</spent> 
    </transaction> 
</statement> 

當我使用控制檯試圖訪問交易對象它仍然未定義任何人都可以指向正確的方向?


UPDATE:

好了,所以基於該回信到目前爲止我的模型現在看起來是這樣的:

var transaction = Ember.ArrayProxy.create({content: []}); 

App.Statement = DS.Model.extend({ 
    all: function() { 
     var transactions = {}; 
     // FROM: http://jquerybyexample.blogspot.com/2012/04/read-and-process-xml-using-jquery-ajax.html 
     $.ajax({ 
      type: "GET", 
      url: "http://www.atwright.co.uk/cof/statement.xml", 
      dataType: "xml", 
      success: function(xml){ 
       var obj = Ember.Object.create({id:null, vendor:null, date:null, spent:null}); 
       obj.setProperties({ 
        id: $(this).attr('id'), 
        vendor: $(this).find('vendor').text(), 
        date: $(this).find('date').text(), 
        spent: $(this).find('spent').text() 
       }); 
       transaction.pushObject(obj); 
      }, 
      error: function() { 
       console.log("An error occurred while processing XML file."); 
      } 
     }); 
     return transactions; 
    } 
}); 

如何訪問任何數據?我可以看到該交易有很多Ember相關的屬性,但沒有數據(儘管我可能會做錯)。

+0

你似乎並不被存儲交易變量的任何地方.. 因爲AJAX請求是異步的,所以你需要將事務存儲在某個地方,這個呃約定是將它們存儲在控制器上的一個'model'變量上。編輯:剛纔看到你在模型內部做AJAX –

+0

我不得不現在暫時擱置 - 所以我會標記一個答案,當我能夠測試等 – atwright147

回答

-1

你沒有一個模型在這裏,因爲你不是擴展Ember.ObjectDS.Model。相反,你可能想是這樣的:

App.Statement = DS.Model.extend({ 
    // Ajax stuff goes here. 
}); 

更多信息,請參見Defining Models guide。您可能還需要看看Creating Custom Transformations,而不是直接在XHR請求執行轉換。

2

我想這個問題(除了異步特殊性)與var transactions = {};是,你只是創建一個普通的JavaScript對象,它具有無燼支持任何由灰燼支持我的意思是,你可以結合等

試圖聲明你喜歡這個交易變量:

var transaction = Ember.ArrayProxy.create({content: []}); 

然後,你遍歷結果(代碼不testet)你的成功的功能內:

... 
var obj = Ember.Object.create({id:null, vendor:null, date:null, spent:null}); 
obj.setProperties({ 
    id: $(this).attr('id'), 
    vendor: $(this).find('vendor').text(), 
    date: $(this).find('date').text(), 
    spent: $(this).find('spent').text() 
}); 
transaction.pushObject(obj); 
... 

希望它可以幫助

0

$.ajax調用是異步的,所以在方法返回時,它返回事務爲空對象。這可能是你在控制檯中看到的。

此外該檢索的記錄的集合最灰燼應用程序返回它們作爲一個數組。這很有用,因爲ArrayController可以輕鬆觀察數組的內容以進行渲染。

+0

做Ember使得JS數組可以是多維的? 我可能做錯了所有的事情,因爲我是一個PHP的傢伙和JS看起來像PHP,但似乎有很多細微的差異。 :S – atwright147

+0

@LukeMelia我在你的回答和我之間看到了一些並行性,只是爲了好奇,如果'transaction'被指定爲一個ember'ArrayProxy',那麼異步行爲會被執行嗎? – intuitivepixel

+0

@ atwright147 JS數組是單維數組。在數組中使有用的方法變得可觀察,但不會改變基本性質。在JS中,'{}'不是一個數組。 –