【问题标题】:Add new record to model from different controller Ember.js从不同的控制器 Ember.js 向模型添加新记录
【发布时间】:2013-10-11 19:38:16
【问题描述】:

我有两个控制器和两个模型。 (我有第三个,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'})
});

【问题讨论】:

    标签: javascript ember.js ember-data


    【解决方案1】:

    我认为你应该能够在你的 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多