CanJS将待办事项存储到文件中

时间:2013-03-08 06:59:57

标签: javascript jquery canjs canjs-model

我正在使用CanJS编写一个SPA,它有一个列表,用户可以添加列表。我想通过将新添加的项目写入文件来永久存储它。有人可以帮助我吗?

'.btn_save click': function (ele, ev) {
        var self = this;
        var val = this.$input.val();
        // get the currently selected model
        var model = this.state.attr('selected');
        // store if it is new
        var isNew = model.isNew();
        // update the attributes
        model.attr('name', val);
        // save it
        model.save(function (library) {
            if (isNew) {
                // if new then append it to the list
                self.libraries.push(library);
            }

            self.resetSelected();
        });
        return false;
    },

这会暂时保存当前浏览会话的项目。我想永久存储它。

2 个答案:

答案 0 :(得分:3)

TodoMVC示例使用localstorage进行永久存储。如果您加入can.localstorage.min.js,可以像这样使用它:

var Todo = can.Model.LocalStorage({
    storageName: 'todos-canjs'
}, {
});

这会将您的模型数据永久存储在浏览器localstorage中。

答案 1 :(得分:0)

您可以使用HTML5 FileWriter API将列表localy保存到文件中。见this question。 除非使用HTML5,否则无法在用户的计算机上使用javascript保存文件。