0

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).

4

3 に答える 3

2

(非同期の特性に加えて)問題var transactions = {};は、Emberサポートがまったくないプレーンなjavascriptオブジェクトを作成しているだけだと思います.Emberサポートにより、バインドできることなど.

次のようにトランザクション変数を宣言してみてください。

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);
...

それが役に立てば幸い

于 2013-05-16T22:44:00.817 に答える
-1

Ember.Objectの代わりに拡張しているため、ここにはモデルがありませんDS.Model。代わりに、おそらく次のようなものが必要です。

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

詳細については、モデルの定義ガイドを参照してください。XHR リクエストで直接変換を実行するのではなく、カスタム変換の作成を参照することもできます。

于 2013-05-16T21:34:51.767 に答える