作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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;
},
这会暂时保存当前浏览 session 的项目。我想永久保存它。
最佳答案
TodoMVC示例使用 localstorage 进行永久存储。如果您包含 can.localstorage.min.js
您可以像这样使用它:
var Todo = can.Model.LocalStorage({
storageName: 'todos-canjs'
}, {
});
这会将您的模型数据永久存储在浏览器本地存储中。
关于javascript - CanJS 将待办事项存储到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15288475/
我是一名优秀的程序员,十分优秀!