模型未保存(夹具)
Model not being saved (Fixtures)
我的记录在创建后没有保存到数据库中。我只是在使用固定装置。在表单中创建时,我可以在 Ember 插件上看到模型,但点击保存后,它们消失了,什么也没有发生。此外,当我 select model.genre select 输入中的一个项目时,什么也没有发生。
这就是我所做的:
/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;
reviews/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>
app/routes/reviews/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();
}
}
}
}
});
app/controllers/reviews/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
任何帮助将不胜感激。谢谢
固定装置是硬编码数据,因此尝试保存它们是行不通的。
它们不在数据库中,它们在文件中 - https://github.com/FranGoitia/reading-list/blob/master/app/models/book.js.
您是否设置了用于保存模型的数据库?
如果您正在使用 ember-cli,请尝试 ember-cli-mirage。
在 ember-cli-mirage 中,您可以拥有类似数据的固定装置,但可以通过虚拟 API 访问它。只要您的应用是 运行.
,您实际上就可以存储数据
我的记录在创建后没有保存到数据库中。我只是在使用固定装置。在表单中创建时,我可以在 Ember 插件上看到模型,但点击保存后,它们消失了,什么也没有发生。此外,当我 select model.genre select 输入中的一个项目时,什么也没有发生。 这就是我所做的:
/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;
reviews/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>
app/routes/reviews/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();
}
}
}
}
});
app/controllers/reviews/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
任何帮助将不胜感激。谢谢
固定装置是硬编码数据,因此尝试保存它们是行不通的。
它们不在数据库中,它们在文件中 - https://github.com/FranGoitia/reading-list/blob/master/app/models/book.js.
您是否设置了用于保存模型的数据库?
如果您正在使用 ember-cli,请尝试 ember-cli-mirage。 在 ember-cli-mirage 中,您可以拥有类似数据的固定装置,但可以通过虚拟 API 访问它。只要您的应用是 运行.
,您实际上就可以存储数据