我有两个Ember型号:a items和comments.用户将发布项目,其他用户将能够对项目发表评论.
我无法在firebase中设置允许name和description仅由当前用户写入的安全规则,但允许comments由任何登录用户写入.
项目
// app/models/item.js
export default DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
comments: DS.hasMany('comment'),
user: DS.belongsTo('user')
})
Run Code Online (Sandbox Code Playgroud)
评论
// app/models/comment.js
export default DS.Model.extend({
user: DS.belongsTo('user')
body: DS.attr('string'),
timestamp: DS.attr('string'),
item: DS.belongsTo('user')
})
Run Code Online (Sandbox Code Playgroud)
保存评论
// app/components/comment-form.js
const comment = this.get('comment');
const item = this.get('item');
// service that tracks the currently logged in user
const currentUser = this.get('sessionManager.user');
comment.set('timestamp', new Date());
comment.set('user', currentUser);
// setup both sides of the relationship
item.get('comments').pushObject(comment);
comment.set('item', …Run Code Online (Sandbox Code Playgroud)