devise - 创建带有实时验证的 Ember 表单的最佳实践是什么?

标签 devise ember.js

我有一个用 Ember + Handlebars 编写的用户注册表。我对两者都是新手。我想在用户键入时对表单执行实时验证。为此,我需要能够观察变化事件并做出智能响应。

我想我可以在我的 View 中声明一个 email 属性并观察它的变化。然后询问服务器该 email 是否已经存在。

我可以想出几种方法来做到这一点,但看不到明显的赢家。我试过观察整个 View 的变化,但这看起来很笨拙。现在我正在尝试创建一个 new-user 对象并将逻辑存储在其中,但 Ember 想要将其连接回服务器。我在这里有点不知所措。

欢迎任何建议。

这是我现在的观点:

FitMap.JoinView = Ember.View.extend({
  templateName: "join",

  name: null,
  email: null,
  password: null,
  password_confirmation: null,

  submit: function(event, view) {
    event.preventDefault();
    event.stopPropagation();

    $.post("/users", {
      name: this.get("name"),
      email: this.get("email"),
      password: this.get("password"),
      password_confirmation: this.get("password_confirmation")
    }, function() {
      console.log("created");
    })
    .fail(function(response) {
      console.log(response);
    });
  }
});

最佳答案

我正在使用 jQuery validation这样

FitMap.JoinView = Ember.View.extend({
  templateName: "join",
  name: null,
  email: null,
  password: null,
  password_confirmation: null,
  didInsertElement: function() {
      var frm = $("#" + this.elementId);
      frm.validate({rules: ...});
  },
  submit: function(event, view) {
    event.preventDefault();
    event.stopPropagation();
    var frm = $("#" + this.elementId);
    if (!frm.valid()) {
        console.log("form is not in a valid state");
        return false;
    }
  })
});

jQuery 验证让您可以在规则中定义远程验证,我正在使用它来检查用户名/电子邮件是否已被使用。

关于devise - 创建带有实时验证的 Ember 表单的最佳实践是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18006350/

相关文章:

sorting - EmberJS Computed.sort - 按关联模型属性排序

html - 如何停止打印媒体的 Bootstrap 表背景颜色设置覆盖我的类(class)设置

ruby-on-rails - 带有 current_user 的 Rails 服务对象

json - 处理路由时出错 : index Assertion Failed: You must include an 'id'

authentication - 在 Rails 3.1.1 中设计,通过种子添加管理员用户?

ruby-on-rails - Rails 4,设计登录规范失败?

javascript - 有没有办法将 Ember 对象转换为普通的 javascript 对象?

javascript - 在 Ember 中对项目集合进行拆分和分组

ruby-on-rails - 设计通过 reset_password_token 获取用户

ruby-on-rails - rails : Devise not properly destroying user session cookie after calling destroy_user_session_path