【问题标题】:Ember.js Save Up and Down Vote to the DataEmber.js 保存对数据的赞成和反对投票
【发布时间】:2014-03-10 05:43:32
【问题描述】:

我使用这个 StackOverflow 问题 (how to handle votes in ember.js?) 中的 jsFiddle (http://jsfiddle.net/chopper/GgGGD/22/) 将上下投票添加到我的 Ember 网站。就像在这个小提琴中一样,我所有的数据都保存在我的 app.js 中,我没有支持的数据库。

上下投票有效,但不会保存该数据,因此会在页面刷新时重置。非常感谢有人支持时如何保存更改的任何帮助!

控制器

App.ThemeController = Ember.ObjectController.extend({
actions: {
    voteUp: function () {
        console.log("voting up");   
        this.set('votes', this.get('votes') + 1);

    },
    voteDown: function () {
        console.log("voting down");
        this.set('votes', this.get('votes') - 1);
    }
}
});

数据

App.THEMES = [
{
id: 1,
title: 'Decode',
price: '$0',
free: true,
description: 'A minimal, modern theme, designed to be mobile first and very responsive, Decode is built just for Ghost and uses Ghost\'s innovative features to present a beautiful and clean blog.',
columns: 1,
popular: true,
purchaseLink:'https://github.com/ScottSmith95/Decode-for-Ghost',
demoLink:'http://decode-ghost-demo.scotthsmith.com',
image: 'images/decode.jpg',
votes: 0
}, ....

谢谢, 大卫·B

【问题讨论】:

标签: javascript ember.js


【解决方案1】:

表格Ember.js guide。

您需要在这些对象上调用 save(),类似这样。

voteDown: function () {
        console.log("voting down");
        this.set('votes', this.get('votes') - 1);
        this.save();
    }

编辑: 但是,如果仅使用您提到的 JSFiddle,它没有任何与数据持久性相关的代码。因此该方法将不起作用。查找 ember-data。

【讨论】:

  • 谢谢,我尝试使用 ember 数据(将 App.whatever 更改为 App.whatever.FIXTURES),但遇到了大量问题,当我刷新页面时数据将不再显示。希望有别的办法。感谢您的回复!也许我会再试一次。
  • 夹具只是示例数据,以帮助开发。您需要配置 ember-data 以使用真实数据存储。
【解决方案2】:

由于您没有持久层,因此您的应用在刷新页面时必然会丢失所有状态。通常,人们使用 ember-data 来进行持久化(更多信息在这里:http://emberjs.com/guides/models/)。如果您不想要后端数据库,则需要使用本地存储。签出 https://github.com/rpflorence/ember-localstorage-adapter 作为 ember-data 的插件以轻松完成此操作。这将允许您在刷新时保持更改,尽管它显然不会跨机器同步。

【讨论】:

  • 非常感谢您的回答,我想我可以只编辑文件本身的代码。感谢您清理它。
猜你喜欢
  • 1970-01-01
  • 2010-12-04
  • 2021-06-19
  • 2013-10-24
  • 2018-07-08
  • 1970-01-01
  • 2012-12-15
  • 2018-06-07
  • 2018-11-05
相关资源
最近更新 更多