[英]Add new record to model from different controller Ember.js
我有兩個控制器和兩個模型。 (我有第三個, groups
,但是在這種情況下無關緊要。)我的模型是這樣關聯的:
App.Favorite = DS.Model.extend({
lodge: DS.belongsTo('lodge'),
notes: DS.attr('string'),
groups: DS.hasMany('group', {async:true}),
photo: DS.attr('string'),
});
App.Lodge = DS.Model.extend({
name: DS.attr('string'),
address: DS.attr('string'),
website: DS.attr('string'),
phone: DS.attr('string'),
uniqueDescription: DS.attr('string'),
basicDescription: DS.attr('string'),
yearOpened: DS.attr('string'),
propertySize: DS.attr('string'),
propertyType: DS.attr('string'),
languages: DS.attr('string'),
owner: DS.attr('string'),
uniqueQualifier1: DS.attr('string'),
uniqueQualifier2: DS.attr('string'),
uniqueQualifier3: DS.attr('string'),
uniqueQualifier4: DS.attr('string'),
lowDayPrice: DS.attr('string'),
highDayPrice: DS.attr('string'),
favoriteBoolean: DS.attr('boolean')
});
我試圖從我的lodge
模板中“分配”該特定旅館以使其成為最愛。 然后,如何從lodge
控制器向其模型添加新的favorite
記錄? 據我所知,Ember指南和API似乎沒有這種用例。
樣品室控制器:
App.LodgeController = Ember.ObjectController.extend({
actions: {
createFavorite: function() {
// Not sure what to do here
//
// Create new record
//
// Then transition to newly created record route
}
}
});
路線:
App.Router.map(function() {
this.resource('groups', { path: '/groups' });
this.resource('group', {path: '/group/:group_id'});
this.resource('favorites', {path: '/favorites'});
this.resource('lodges', {path: '/'});
this.resource('favorite', {path:'/favorite/:favorite_id'})
});
我認為您應該可以在createFavorite
函數中執行以下操作:
var lodge = this.get('content');
var fav = this.store.createRecord('farvorite',{ lodge : lodge });
var _this = this; // Inside didCreate 'this' will point to something different
fav.on('didCreate',function(){
_this.transitionToRoute('favorite',fav);
});
fav.save();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.