简体   繁体   English

Ember.js属于选择关系中的创建/编辑选择元素(下拉列表)

[英]Ember.js belongsTo relationship create/edit in select element (dropdown)

I'm attempting to set the belongsTo relationship using a dropdown. 我正在尝试使用下拉菜单设置belongsTo关系。

So I have my Books model: 所以我有我的书籍模型:

import DS from 'ember-data';

export default DS.Model.extend({
  // Relationships
  author: DS.belongsTo('author'),
  name: DS.attr()
});

And my Author model: 和我的作者模型:

import DS from 'ember-data';

export default DS.Model.extend({
  // Relationships
  author: DS.hasMany('books'),
  name: DS.attr()
});

My Books/new route: 我的书/新路线:

import Ember from 'ember';

export default Ember.Route.extend({

  model() {
    return Ember.RSVP.hash({
      book: this.store.createRecord('book'),
      authors: this.store.findAll('author')
    })
  },

  actions: {

    saveBook(newBook) {
      newBook.book.save().then(() => this.transitionTo('book'));

    },

    willTransition() {
      this.controller.get('book').rollbackAttributes();
    }
  }
});

And my Books/new template: 和我的书/新模板:

<label >Book Name</label>
{{input type="text" value=model.name placeholder="Book Name"}}

<label>Author</label>
<select>
  {{#each model.authors as |author|}}
    <option value="{{author.id}}">
      {{author.name}}
    </option>
  {{/each}}
</select>
<button type="submit"{{action 'saveBook' model}}>Add Book</button>

If I remove the select element and just save the name of the book it works fine, but with it I get this: (where id is an auto-generated ID) 如果删除select元素并仅保存书名,则可以正常工作,但是有了它,我得到了:(其中id是自动生成的ID)

Error: Some errors were encountered while saving app@model:book id
at reportError (firebase.js:425)
at firebase.js:445
at tryCatch (ember.debug.js:58165)
at invokeCallback (ember.debug.js:58177)
at publish (ember.debug.js:58148)
at publishRejection (ember.debug.js:58091)
at ember.debug.js:37633
at invoke (ember.debug.js:339)
at Queue.flush (ember.debug.js:407)
at DeferredActionQueues.flush (ember.debug.js:531)

I think I need to do something like getting the author object and setting book.author to that, but I can't find a clear explanation of how. 我想我需要做一些事情,例如让author对象和将book.author设置为该对象,但是我找不到关于它的清晰解释。 Especially as I can't even work out how to get the data from that select menu in the route! 特别是因为我什至无法弄清楚如何从路线中的选择菜单中获取数据!

I feel like I'm missing something pretty simple here, anyone have any insight? 我觉得我在这里错过了一些简单的事情,任何人都有见识?

I would suggest moving this functionality to your controller.js where it belongs. 我建议将此功能移到它所属的controller.js中。 Why is your relation to books in AuthorModel called author instead of books ? 为什么您与AuthorModel中的书的关系称为author而不是books I would suggest rewriting your action (in the controller) to something like this: 我建议将您的操作(在控制器中)重写为类似以下内容:

saveBook(newBook) {
  newBook.set('author', this.get('selectedAuthor') // or just the call below if you go with the alternative below
  newBook.save().then(() => this.transitionTo('book'));

},

Now the problem persists, that you don't have a binding to your selected author. 现在问题仍然存在,您没有与所选作者的绑定。 I would suggest using something like ember-power-select to bind your selected author to a controller property. 我建议使用诸如ember-power-select之类的东西将您选择的作者绑定到控制器属性。

Then you would do this in your template: 然后,您可以在模板中执行此操作:

{{#power-select
    placeholder="Please select Author"
    onchange=(action "authorSelectionChanged")
    options=model.authors
    as |author|}}
    {{author.name}}
{{/power-select}}

And in your actions within your controller: 并在您的控制器中actions

authorSelectionChanged(author) {
    this.get('model.book').set('author', author);
    // or the following if you go with the alternative above
    this.set('selectedAuthor', author);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM