Question

I am writing a SPA using CanJS which has a list and user can add the list. I want to store the newly added item permanently by writing it to a file. Can some one help me?

'.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;
    },

This saves the item temporarily for the current browsing session. I want to store it permanantly.

Was it helpful?

Solution 2

You can save your list localy to a file using HTML5 FileWriter API. See this question. Unless using HTML5, it's not possible to save file using javascript on user's computer.

OTHER TIPS

The TodoMVC example uses localstorage for permanent storage. If you include can.localstorage.min.js you can use it like this:

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

This will store your model data permanently in the browsers localstorage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top