0

我是 emberjs (1.0.0-RC1) 的新手,我在 Rails 上使用它。我想在不使用 ember 模型的情况下提交会话表单。我觉得这更好,因为我的 rails 应用程序中没有真正的会话模型。目前有效的方法如下:

#login_controller.js.coffee
SkillUp.LoginController = Ember.ObjectController.extend
  # Just a title to pass to the template, for fun
  title: "Login Controller"

  submit: (controller) ->
    model = @get 'model'
    model.get('transaction').commit()

#session.js.coffee
SkillUp.Session = DS.Model.extend
  email: DS.attr 'string'
  password: DS.attr 'string'

#login_route.js.coffee
SkillUp.LoginRoute = Ember.Route.extend
  setupController: (controller) ->
    controller.set 'title', "Login"

    controller.set 'content', SkillUp.Session.createRecord()

<!-- login.handlebars -->
<h2>template: {{title}}</h2>
<form>
  {{#with controller}}
    email: {{view Ember.TextField valueBinding="email"}}
    password: {{view Ember.TextField valueBinding="password" type="password"}}
    <button {{action submit}}>Submit</button>
  {{/with}}
</form>

正如我所说,我的目标是删除 session.js.coffee,因为它不存在 rails 模型。

帮助将不胜感激谢谢

4

2 回答 2

1

我不建议$.ajax直接实施。我建议保留 DS 模型。如果您使用DS.FixtureAdapter. 理想情况下,您应该将所有 ajax 保留在应用程序的一个层后面。您不应该$.ajax插入应用程序的各个部分。

编辑:仅仅因为您在后端没有 Session 模型并不意味着您不能在前端拥有一个。ember 应用程序将拥有自己的模型。不用担心 1:1 映射。

于 2013-03-02T07:00:51.263 回答
0

您可以在控制器方法中使用 $.ajax 手动使用 jQuery 将值发布到 rails。

$.ajax('/sessions', {
    type: 'POST',
    data: {
        email: this.get('email'),
        password: this.get('password)
    },
    ...
});

为成功和失败添加适当的处理程序。

于 2013-03-02T02:38:17.687 回答