I am a CodeIgniter dev and I am trying to learn ember.js.
I have been trying to get a model together which will make an AJAX call to a server, pull in a XML file, parse the data and return it.
The following is my model code:
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;
}
});
The following is the content of my XML file (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>
When I use the console to try to access the transactions object it remains undefined can anyone point me in the right direction?
UPDATE:
Okay, so based on the replys so far my model now looks like this:
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;
}
});
How do I access any of the data? I can see that transaction has many Ember related properties but no data within it (though I could be doing it wrong).