0

用户可以通过表单域进行操作。但我不知道如何保存(提交)它们。当我更改lastNameaUser并单击保存按钮时,我会收到以下错误:

Uncaught Error: Attempted to handle event `save` on <App.User:ember309:1> while in state rootState.loaded.updated.uncommitted. Called with undefined

通过单击保存按钮,我必须更改哪些内容才能保存(提交)记录?

索引.html

<script type="text/x-handlebars" data-template-name="users">
  <table class='table'>
      {{#each model}}
        <tr>
          <td>{{view Ember.TextField valueBinding='lastName'}}</td>
          {{#with this as model}}
            <td><button {{bindAttr class=":btn isDirty:btn-primary:disabled"}} {{action "save" target="model"}}>save</button></td>
          {{/with}}
        </tr>
      {{/each}}
  </table>
</script>

应用程序.js

App = Ember.Application.create();

App.Store = DS.Store.extend({
  revision: 12,
  adapter: 'DS.FixtureAdapter'
})

App.Router.map(function() {
  this.resource('users', function() {
    this.resource('user', { path: ':user_id' })
  })
});

App.UsersRoute = Ember.Route.extend({
  model: function() {
    return App.User.find();
  }
});

App.UserController = Ember.ObjectController.extend({
  save: function() {
    this.commit();    
  }  
})

App.User = DS.Model.extend({
  lastName: DS.attr('string')
})

App.User.FIXTURES = [{
  id: 1,
  lastName: "Smith"
}]
4

1 回答 1

1

您可以像这样更改您的代码

<script type="text/x-handlebars" data-template-name="users">
  <table class='table'>
      {{#each model in controller}}
        <tr>
          <td>{{view Ember.TextField valueBinding='lastName'}}</td>
          <td><button {{bindAttr class=":btn isDirty:btn-primary:disabled"}} {{action "save"}}>save</button></td>
        </tr>
      {{/each}}
  </table>
</script>

使用each model in controller而不是#with this as model

然后你定义一个itemController

App.UsersController = Ember.ArrayController.extend({
  itemController: 'user'
});

这将允许您拥有被调用的save方法。UserController

最后,确保保存您的记录:

App.UserController = Ember.ObjectController.extend({
  save: function() {
    this.get('content.transaction').commit();
  }
})
于 2013-04-20T17:26:23.627 回答