1

開発にフレームワークを使用するのはこれが初めてで、最初のステップに固執しています。

Flex アプリケーションを Javascript アプリケーションに変換し、バックボーンをフレームワークとして使用しています。

名前値形式のテキスト ファイルをロードする必要があります。

<!doctype html>
<html>
<head>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.10/backbone-min.js'></script>
<script>
var ResourceBundleCollection = Backbone.Collection.extend({
url:'ResourceBundle.txt',
});
var resourceBundleCollection = new ResourceBundleCollection();
resourceBundleCollection.fetch();
</script>
</head>
<body>
</body>
</html>

ResourceBundle.txt には、次の形式のコンテンツが含まれています

location_icon=../../abc/test.png
right_nav_arrow_image=assets/images/arrow.png
right_nav_arrow_image_visible=true

整形式ではない次のエラーがスローされます

JQueryを使用してテキストファイルを簡単にロードして解析できました

$.ajax({
type : "GET",
url : "ResourceBundle.txt",
datatype : "script",
success : resourceXMLLoaded
});

次のコードを使用して解析します

var lines = txt.split("\n");
for(var i=0;i<lines.length;i++) {
if(lines[i].length > 5) {
var _arr = lines[i].split("=");
resourceBundleObj[$.trim(_arr[0])] = $.trim(_arr[1]);
}
}

backbone.js で同じ結果を得る方法を教えてください。

4

2 に答える 2

5

これをサポートするためにプレーン テキストを使用する必要がある場合は、オーバーライドBackbone.Collection.parseして必要なものを実現できます。

それに加えて、ResourceBundleModel内の各アイテムをホストする を作成することもできますResourceBundleCollection

ここでデモを見ることができます: http://jsfiddle.net/dashk/66nkF/

モデルとコレクションのコードは次のとおりです。

// Define a Backbone.Model that host each ResourceBundle
var ResourceBundleModel = Backbone.Model.extend({
    defaults: function() {
        return {
            name: null,
            value: null
        };
    }
});

// Define a collection of ResourceBundleModels.
var ResourceBundleCollection = Backbone.Collection.extend({
    // Each collection should know what Model it works with, though
    // not mandated, I guess this is best practice.
    model: ResourceBundleModel,

    // Replace this with your URL - This is just so we can demo
    // this in JSFiddle.
    url: '/echo/html/',

    parse: function(resp) {
        // Once AJAX is completed, Backbone will call this function
        // as a part of 'reset' to get a list of models based on
        // XHR response.
        var data = [];
        var lines = resp.split("\n");

        // I am just reusing your parsing logic here. :)
        for (var i=0; i<lines.length; i++) {
            if (lines[i].length > 5) {
                var _arr = lines[i].split("=");

                // Instead of putting this into collection directly,
                // we will create new ResourceBundleModel to contain
                // the data.
                data.push(new ResourceBundleModel({
                    name: $.trim(_arr[0]),
                    value: $.trim(_arr[1])
                }));
            }    
        }

        // Now, you've an array of ResourceBundleModel. This set of
        // data will be used to construct ResourceBundleCollection.
        return data;
    },

    // Override .sync so we can demo the feature on JSFiddle
    sync: function(method, model, options) {
        // When you do a .fetch, method is 'read'
        if (method === 'read') {
            var me = this;

            // Make an XHR request to get data
            // Replace this code with your own code
            Backbone.ajax({
                url: this.url,
                method: 'POST',
                data: {
                    // Feed mock data into JSFiddle's mock XHR response
                    html: $('#mockData').text()
                },
                success: function(resp) {
                    options.success(me, resp, options);
                },
                error: function() {
                    if (options.error) {
                        options.error();
                    }
                }
            });
        }
        else {
            // Call the default sync method for other sync method
            Backbone.Collection.prototype.sync.apply(this, arguments);
        }
    }
});

Backbone は、ネイティブで JSON を介して RESTful API と連携するように設計されています。ただし、十分なカスタマイズがあれば、必要に応じて柔軟に対応できるライブラリです。

于 2013-02-12T13:05:09.050 に答える
2

デフォルトでは、バックボーンのコレクションは JSON 形式のコレクションのみを受け入れます。したがって、入力を JSON 形式に変換する必要があります。

[{"name": "name", "value": "value},
 {"name": "name", "value": "value}, ...
]

もちろん、デフォルトの動作をオーバーライドできます: Overriding backbone's parse function

于 2013-02-12T11:36:53.847 に答える