3

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

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

我可以想出几种方法来做到这一点,但看不到明显的赢家。我试过观察整个视图的变化,但这似乎很笨拙。现在我正在尝试创建一个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);
    });
  }
});
4

1 回答 1

2

我以这种方式使用jQuery 验证

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 验证让您在规则中定义远程验证,我正是在使用它来检查用户名/电子邮件是否已在使用中。

于 2013-08-02T14:59:04.877 回答