从不同的控制器 Ember.js 向模型添加新记录
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();
相关文章:
- 为什么将子模型添加到两个父模型
- 主干.js:为每个模型添加验证
- 调用ko.applyBindings后,向Knockout视图模型添加新属性
- 第一步's与THRE.js:试图将搅拌机模型添加到场景中的问题
- 在knocket js中应用绑定后,向视图模型添加新属性
- 创建 Dojo 网格并从数据模型添加对话框
- 向骨干模型添加更多功能
- 主干:集合将随机模型添加到自身
- 将模型添加到木偶项视图定义之外的主干集合
- 将模型添加到主干.js收集以静默方式失败
- 从不同的控制器 Ember.js 向模型添加新记录
- 确保不会将重复的模型添加到集合中
- 主干.js:在将模型添加到集合之前不进行工作验证
- 将新模型添加到主干集合,而不是替换
- 正在将模型添加到具有索引的集合中
- 主干:将模型添加到集合并渲染视图
- 允许使用向asp.net mvc4模型添加项目
- 无法将模型添加到主干集合
- Backbone.js将从服务器检索到的模型添加到集合中
- 将模型添加到集合不起作用