骨干文件列表作为数组

时间:2014-02-15 10:29:51

标签: javascript backbone.js underscore.js

我是骨干的新手。

我正在尝试从REST API加载文件夹的文件列表,并将其作为数组保存到localstorage,然后使用localStorage数据呈现视图。

我正在使用this骨干脱机适配器

我设法将数据从REST加载到localStorage,但我无法将加载的数据保存为ARRAY。

它为localStorage中的每个文件创建多个记录。

这就是我得到的,

file-1  {"id":"1", "folderid":"0001","name":"file number one","size":"1000"}
file-2  {"id":"2", "folderid":"0001","name":"file number two","size":"1000"}
file-3  {"id":"3", "folderid":"0001","name":"file number three","size":"1000"}
file-4  {"id":"4", "folderid":"0001","name":"file number four","size":"1000"}
file-5  {"id":"5", "folderid":"0001","name":"file number five","size":"1000"}

但这就是我需要的

我只需要在localStorage中为每个文件夹创建一条记录。

file-0001    [{"id":"1", "folderid":"0001","name":"file number one","size":"1000"}, {"id":"2", "folderid":"0001","name":"file number two","size":"1000"},{"id":"3", "folderid":"0001","name":"file number three","size":"1000"},{"id":"4", "folderid":"0001","name":"file number four","size":"1000"},{"id":"5", "folderid":"0001","name":"file number five","size":"1000"}]

更新:

directory.FILES = Backbone.Model.extend({});
directory.FOLDER = Backbone.Collection.extend({

    initialize: function() {
        return this.storage = new Offline.Storage("folder-files", this, {
            autoPush: !0
        })
    },
    url: "api/folder/files", //this will return json data [{"id":"1", "folderid":"0001"},{...},{...}]

    model: directory.FILES
});

    var FOLDER = new directory.FOLDER();
    FOLDER.fetch();

0 个答案:

没有答案