在Ember.JS中保存表单数据 - 使用2.3版

时间:2016-02-29 11:04:26

标签: ember.js

有这篇文章:" What's the right way of doing manual form data saving with Ember.js?"但是,我对如何使用Ember 2.3做不了解。

在该问题的accepted answer中,我们有以下代码段:

App.IndexController = Ember.ObjectController.extend({
  proxy: {},
  setUnknownProperty: function(key, value) {
    console.log("Set the unknown property: " + key + " to: " + value); 
    this.proxy[key] = value;
    console.log(this.proxy);
  },
  flush: function() {
    for(var key in this.proxy) 
      this.set('model.'+key, this.proxy[key]);
  }
}); 

然而,我没有" ObjectController"在我的项目中。运行

ember generate controller test

给了我一些读取Ember.Controller.extend({ ... });而不是ObjectController的东西。搜索2.3 API,我根本找不到ObjectController。

将片段插入Ember.Controller.extend,我的各种操作方法放在哪里似乎没有办法。整个模型消失(无数据),添加数据也不起作用。什么都没发生,没有错误,没有。这些方法可能根本就没有被调用。

关于"转换"的任何建议这个到2.3将不胜感激。

现在我有

export default Ember.Controller.extend({
proxy: {},
  actions: {
    setUnknownProperty: function(key, value) {
              console.log("Set the unknown property: " + key + " to: " + value);
              this.proxy[key] = value;
              console.log("Proxy: ");
              console.log(this.proxy);
          },
          testAction: function() {
              console.log("fired");
          }, flush: function() {
                   console.log("Flush called");
                      for(var key in this.proxy) {
                        console.log("Flushing " + key);
                        this.set('model.'+key, this.proxy[key]);
                        console.log("Saving");
                        this.get('model.'+key).save();
                    }
          }
}

1 个答案:

答案 0 :(得分:1)

要回答这个问题,我创建了一个快速示例应用程序,可在此处查看:

https://github.com/tyronepost/EmberFormExample

我使用以下命令创建了路径索引路由和用户模型:

root@monkey:/tmp/events-log# buck build plugin
[-] PROCESSING BUCK FILES...FINISHED 0.3s [100%]
[+] DOWNLOADING... (0.00 B/S, TOTAL: 0.00 B, 0 Artifacts)
[+] BUILDING...0.2s [19%] (3/19 JOBS, 0 UPDATED, 0.0% CACHE MISS)
|=> //lib/commons:dbcp__download_bin...  0.1s (running genrule[0.0s])
Traceback (most recent call last):
File ".bootstrap/_pex/pex.py", line 320, in execute
File ".bootstrap/_pex/pex.py", line 253, in _wrap_coverage
File ".bootstrap/_pex/pex.py", line 285, in _wrap_profiling
File ".bootstrap/_pex/pex.py", line 363, in _execute
File ".bootstrap/_pex/pex.py", line 421, in execute_entry
File ".bootstrap/_pex/pex.py", line 426, in execute_module
File "/usr/lib/python2.7/runpy.py", line 180, in run_module
  fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
  exec code in run_globals
File "bucklets/tools/download_file.py", line 198, in <module>
File "/usr/lib/python2.7/subprocess.py", line 535, in check_call
  retcode = call(*popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 522, in call
  return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
  errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
  raise child_exception
OSError: [Errno 2] No such file or directory

索引/ template.hbs:

ember g route index -p // -p short for --pod, groups .js and .hbs file into the same dir
ember g model user

索引/ route.js:

<div>first name: {{input value=firstName}}</div>
<div>last name: {{input value=lastName}}</div>
<div>email: {{input value =email}}</div>
<button {{action 'submit'}}>submit</button>

user.js的:

import Ember from 'ember';

export default Ember.Route.extend({
  actions: {
    submit: function() {
      var user = this.store.createRecord('user', {
        firstName: this.controller.get('firstName'),
        lastName: this.controller.get('lastName'),
        email: this.controller.get('email')
      });
      user.save().then( () => {
        console.log('save successful');
        this.controller.set('firstName', null);
        this.controller.set('lastName', null);
        this.controller.set('email', null);
      }, function() {
        console.log('save failed');
      });
    }
  }
});

我还创建了一个rest适配器并将命名空间指定为&#39; api&#39;:

import DS from 'ember-data';

export default DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  email: DS.attr('string')
});

的application.js

ember g adapter application

一个http-mock对象,当&#39; ember server&#39;被称为

import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  namespace: 'api'
});

这里要注意的重要一点是我们正在使用路由映射到的默认控制器,因此不需要显式创建一个。在这个例子中,我们使用它的所有内容都是维护输入字段的状态,直到我们点击提交。

在我们的提交函数中,我们调用user.save()。then(...),它将成功回调和失败回调作为参数。执行一个取决于服务器如何响应。

以下这本书是关于如何处理余烬的细微差别的非常好的信息来源.x

https://pragprog.com/book/mwjsember/deliver-audacious-web-apps-with-ember-2