将列添加到现有 ember 模型
Add column to existing ember model
哇,这个好难找啊。
我在 ember 中有一个现有模型,我想添加一个新列。我无法看到如何从 CLI 生成它,所以手动将它添加到我的 component.js
和 models/post.js
。我已将该字段添加到我的表单中,并将车把添加到我的视图中。检查 Firebase 我可以确认我没有更新该字段。
在 Rails 中我会简单地 运行 rails generate migration add_snippet_to_posts snippet:string
但在 Ember 中这样做只会创建一个新模型。
model/post.js
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
author: DS.attr('string'),
createdDate: DS.attr('date'),
text: DS.attr('string'),
snippet: DS.attr('string') #<=manually added this.
});
component.js
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
createPost: function (model) {
this.sendAction('createPost', model);
// Clear each input field
this.set('newPost.title', null);
this.set('newPost.author', null);
this.set('newPost.text', null);
this.set('newPost.snippet', null); #<= manually added this
}
}
});
我该怎么做?
已解决
也需要更新 routes/index.js:
import Ember from 'ember';
export default Ember.Route.extend({
model: function () {
return this.store.findAll('post');
},
actions: {
createPost: function (model) {
let post = this.store.createRecord('post', {
title: model.title,
text: model.text,
author: model.author,
snippet: model.snippet, # <= add this too
createdDate: new Date()
});
post.save();
}
}
});
官方的回答是,您不能只使用 ember-CLI 添加属性 到已经创建的模型 - 并且 同时,更新它可能影响整个应用程序的所有内容。您必须手动编写属性及其在 routes/components/templates 等
中的使用方式
Rails 可以知道所有这些东西,这听起来很棒。 :)
哇,这个好难找啊。
我在 ember 中有一个现有模型,我想添加一个新列。我无法看到如何从 CLI 生成它,所以手动将它添加到我的 component.js
和 models/post.js
。我已将该字段添加到我的表单中,并将车把添加到我的视图中。检查 Firebase 我可以确认我没有更新该字段。
在 Rails 中我会简单地 运行 rails generate migration add_snippet_to_posts snippet:string
但在 Ember 中这样做只会创建一个新模型。
model/post.js
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
author: DS.attr('string'),
createdDate: DS.attr('date'),
text: DS.attr('string'),
snippet: DS.attr('string') #<=manually added this.
});
component.js
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
createPost: function (model) {
this.sendAction('createPost', model);
// Clear each input field
this.set('newPost.title', null);
this.set('newPost.author', null);
this.set('newPost.text', null);
this.set('newPost.snippet', null); #<= manually added this
}
}
});
我该怎么做?
已解决
也需要更新 routes/index.js:
import Ember from 'ember';
export default Ember.Route.extend({
model: function () {
return this.store.findAll('post');
},
actions: {
createPost: function (model) {
let post = this.store.createRecord('post', {
title: model.title,
text: model.text,
author: model.author,
snippet: model.snippet, # <= add this too
createdDate: new Date()
});
post.save();
}
}
});
官方的回答是,您不能只使用 ember-CLI 添加属性 到已经创建的模型 - 并且 同时,更新它可能影响整个应用程序的所有内容。您必须手动编写属性及其在 routes/components/templates 等
中的使用方式Rails 可以知道所有这些东西,这听起来很棒。 :)