模型未保存(灯具)

时间:2015-06-02 14:10:52

标签: ember.js ember-data fixtures

我的recors在创建后没有保存到数据库中。我只是在使用灯具。我可以在表格中创建它时看到Ember插件上的模型,但是在点击保存之后,它们会消失,没有任何反应。此外,当我在model.genre上选择一个项目时,选择输入没有任何反应。 这就是我所做的:

/router.js

Router.map(function() {
  this.resource('books', {path: '/'});
  this.resource('book', {path: '/books/:book_id'});
  this.resource('genre', {path: '/genres/:genre_id'});

  this.resource('reviews', function() {
    this.route('new');
  });
});

export default Router;

评论/ new.hbs

<div class='row'>
    <form class="form-horizontal" role="form">
      <div class="form-group">
        <label for="book_title" class="col-sm-2 control-label">Book Title</label>
        <div class="col-sm-9">
          {{input class="form-control" id="book_title" placeholder="Book Title" valueBinding="model.title"}}
        </div>
      </div>
      <div class="form-group">
        <label for="book_author" class="col-sm-2 control-label">Author</label>
        <div class="col-sm-9">
          {{input class="form-control" id="book_author" placeholder="Author" valueBinding="model.author"}}
        </div>
      </div>
      <div class="form-group">
        <label for="book_amazon_id" class="col-sm-2 control-label">Amazon ID</label>
        <div class="col-sm-9">
          {{input class="form-control" id="book_amazon_id" placeholder="Amazon ID" valueBinding="model.amazon_id"}}
        </div>
      </div>
      <div class="form-group">
        <label for="book_review" class="col-sm-2 control-label">Review</label>
        <div class="col-sm-9">
          {{textarea valueBinding="model.review" class="form-control" id="book_review"}}
        </div>
      </div>
      <div class="form-group">
        <label for="book_rating" class="col-sm-2 control-label">Rating</label>
        <div class="col-sm-9">
          {{view Ember.Select content=ratings valueBinding='model.rating' class="form-control" id="book_rating"}}
        </div>
      </div>
      <div class="form-group">
        <label for="book_genre" class="col-sm-2 control-label">Genre</label>
        <div class="col-sm-9">
          {{view Ember.Select content=genres optionLabelPath="content.name" valueBinding="model.genre" class="form-control" id="book_genre"}}
        </div>
      </div>

      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button type="submit" class="btn btn-default" {{action 'createReview'}}>Save Review</button>
        </div>
      </div>
    </form>
  </div>

  {{book-details book=model}}
</div>

应用/路由/评论/ new.js

export default Ember.Route.extend({
  model: function() {
    return Ember.RSVP.hash({
      book: this.store.createRecord('book'),
      genres: this.store.findAll('genre')
    });
  },
  setupController: function(controller, model) {
    controller.set('model', model.book);
    controller.set('genres', model.genres);
  },
  actions: {
    willTransition: function(transition) {
      if(this.currentModel.book.get('isNew')) {
        if(confirm("Are you sure you want to abandon progress?")) {
          this.currentModel.book.destroyRecord();
        } else {
          transition.abort();
        }
      }
    }
  }
});

应用/控制器/评论/ new.js

export default Ember.Controller.extend({
  ratings: [5,4,3,2,1],
  actions: {
    createReview: function() {
      var controller = this;
      this.get('model').save().then(function() {
        controller.transitionToRoute('books');
      });
    }
  }
});

如果有帮助,这是完整的代码:https://github.com/FranGoitia/reading-list

任何帮助都会非常感激。感谢

2 个答案:

答案 0 :(得分:1)

灯具是硬编码的数据,所以试图保存它们不会起作用 它们不在数据库中,它们位于文件中 - https://github.com/FranGoitia/reading-list/blob/master/app/models/book.js 您是否设置了数据库以将模型保存到?

答案 1 :(得分:0)

如果您使用的是ember-cli,请尝试ember-cli-mirage。 在ember-cli-mirage中,您可以拥有类似数据的夹具,但可以通过虚拟API访问。只要您的应用正在运行,您就可以存储数据。