这是一个新手问题:我正在商店(Ember-data)中创建一个新的章节记录。我想将章节 ID 添加到用户模型记录的 User.chapters 属性中。
App.Book = DS.Model.extend({
name: DS.attr('string'),
icon: DS.attr('string'),
description: DS.attr('string'),
createdDate: DS.attr('date'),
chapters: DS.hasMany('chapter', { inverse: 'book', async: true}),
bookAssignments: DS.hasMany('bookAssignment', {async: true}),
user: DS.belongsTo('user', { inverse: 'books', async: true} ),
});
App.Chapter = DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
source: DS.attr('string'),
book: DS.belongsTo('book', { inverse: 'chapters', async: true}),
user: DS.belongsTo('user', { inverse: 'chapters', async: true} )
});
App.User = DS.Model.extend({
uid: DS.attr('string'),
displayName: DS.attr('string'),
firstName: DS.attr('string'),
lastName: DS.attr('string'),
books: DS.hasMany('condition', { inverse: 'user', async: true}),
chapters: DS.hasMany('statistic', { inverse: 'user', async: true}),
});
我创建了一个注入到路由和控制器中的 Session 对象
App.Session = Ember.Object.extend({
user: null,
userID: '',
});
Ember.Application.initializer({
initialize: function (container, application) {
App.register('session:main', App.Session.create(), { instantiate: false, singleton: true });
// Add `session` object to route to check user
App.inject('route', 'session', 'session:main');
// Add `session` object to controller to visualize in templates
App.inject('controller', 'session', 'session:main');
}
});
但是,Session 对象不是 Model,无法使用 Save() 方法保存到 Store:
user.save()
我探索的另一个选项是从用户路由模型挂钩访问用户记录。用户路线:
App.UserRoute = Ember.Route.extend({
model: function() {
//var session = this.get('session');
var user = '';
var _isAutheticated = this.isAutheticated;
var store = this.store;
var _firebase = new Firebase("https://dynamicslife.firebaseio.com");
_firebase.onAuth(function(authData) {
if (authData) {
_isAutheticated = true;
console.log('user authenticated with Firebase', authData.uid);
user = store.find('user', authData.uid).then(function(_user) {
console.log('user found', _user);
return _user;
}, function(error) {
// user does not exist or some other error
console.log('please log again');
});
} else if (error) {
_isAutheticated = false;
console.log('failled login attempt', error);
}
}); //end authWithOAuthPopup
console.log('My user2: ', user);
//this.set('isAutheticated', _isAutheticated);
return user;
}
});
我尝试需要从章节控制器访问用户记录:
App.ChaptersController = Ember.ArrayController.extend({
needs: 'user',
user: Ember.computed.alias("controllers.user.model"),
});
但是,用户仍然不可用。
我想象我的语法不正确。如果有人能告诉我一种访问用户记录的正确方法,我将不胜感激,这种方法可以从章节控制器修改和保存。
Chapters 路由没有嵌套在 User 路由下,因为模板没有嵌套。即使模板没有嵌套使用 ModelFor(),我是否应该在用户路由下嵌套章节路由?